Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types(runtime-core): enable plugin option types #3969

Merged
merged 7 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/runtime-core/src/apiCreateApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { ObjectEmitsOptions } from './componentEmits'
export interface App<HostElement = any> {
version: string
config: AppConfig
use(plugin: Plugin, ...options: any[]): this
use<Option>(plugin: Plugin<Option>, ...options: Option[]): this
mixin(mixin: ComponentOptions): this
component(name: string): Component | undefined
component(name: string, component: Component): this
Expand Down Expand Up @@ -137,12 +137,12 @@ export interface AppContext {
filters?: Record<string, Function>
}

type PluginInstallFunction = (app: App, ...options: any[]) => any
type PluginInstallFunction<Option> = (app: App, ...options: Option[]) => any

export type Plugin =
| (PluginInstallFunction & { install?: PluginInstallFunction })
export type Plugin<Option = any> =
| PluginInstallFunction<Option> & { install?: PluginInstallFunction<Option> }
| {
install: PluginInstallFunction
install: PluginInstallFunction<Option>
}

export function createAppContext(): AppContext {
Expand Down
70 changes: 70 additions & 0 deletions test-dts/appUse.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { createApp } from './index'

type App = ReturnType<typeof createApp>

type PluginAOptionType = {
option1?: string
option2: number
option3: boolean
}

const PluginA = {
install(app: App, ...options: PluginAOptionType[]) {
tony19 marked this conversation as resolved.
Show resolved Hide resolved
options[0].option1
options[0].option2
options[0].option3
}
}

const PluginB = {
install(app: App) {}
}

const PluginC = (app: App, ...options: string[]) => {}

createApp({})
.use(PluginA)
// @ts-expect-error option2 and option3 (required) missing
.use(PluginA, {})
// @ts-expect-error type mismatch
.use(PluginA, true)
// @ts-expect-error type mismatch
.use(PluginA, undefined)
// @ts-expect-error type mismatch
.use(PluginA, null)
// @ts-expect-error type mismatch
.use(PluginA, 'foo')
// @ts-expect-error type mismatch
.use(PluginA, 1)
.use(PluginA, { option2: 1, option3: true })
.use(PluginA, { option1: 'foo', option2: 1, option3: true })

// @ts-expect-error option2 (required) missing
.use(PluginA, { option3: true })

.use(PluginB)
// @ts-expect-error unexpected plugin option
.use(PluginB, {})
// @ts-expect-error unexpected plugin option
.use(PluginB, true)
// @ts-expect-error unexpected plugin option
.use(PluginB, undefined)
// @ts-expect-error unexpected plugin option
.use(PluginB, null)
// @ts-expect-error type mismatch
.use(PluginB, 'foo')
// @ts-expect-error type mismatch
.use(PluginB, 1)

.use(PluginC)
posva marked this conversation as resolved.
Show resolved Hide resolved
// @ts-expect-error unexpected plugin option
.use(PluginC, {})
// @ts-expect-error unexpected plugin option
.use(PluginC, true)
// @ts-expect-error unexpected plugin option
.use(PluginC, undefined)
// @ts-expect-error unexpected plugin option
.use(PluginC, null)
.use(PluginC, 'foo')
// @ts-expect-error type mismatch
.use(PluginC, 1)