Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
71 changes: 71 additions & 0 deletions test-dts/appUse.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { createApp } from './index'

type App = ReturnType<typeof createApp>

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

const PluginA = {
install(app: App, ...options: PluginAOptionType[]) {
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 })
.use(PluginA, { option1: 'foo', option2: 1, option3: true }, { 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)
// @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)