From 12a56bd7462c55172c6aa8b8f2da84082524a68f Mon Sep 17 00:00:00 2001 From: fnlCtrl Date: Tue, 8 Dec 2020 11:26:04 +0800 Subject: [PATCH 1/2] feat: improve plugin params typing --- packages/runtime-core/src/apiCreateApp.ts | 7 +++- test-dts/apiCreateApp.test-d.ts | 42 +++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 test-dts/apiCreateApp.test-d.ts diff --git a/packages/runtime-core/src/apiCreateApp.ts b/packages/runtime-core/src/apiCreateApp.ts index bcb2fec1c32..8dd163b4729 100644 --- a/packages/runtime-core/src/apiCreateApp.ts +++ b/packages/runtime-core/src/apiCreateApp.ts @@ -19,7 +19,12 @@ import { version } from '.' export interface App { version: string config: AppConfig - use(plugin: Plugin, ...options: any[]): this + use( + plugin: T, + ...options: T extends { install: (app: App, ...options: infer R) => any } + ? R + : T extends ((app: App, ...options: infer R) => any) ? R : any + ): this mixin(mixin: ComponentOptions): this component(name: string): Component | undefined component(name: string, component: Component): this diff --git a/test-dts/apiCreateApp.test-d.ts b/test-dts/apiCreateApp.test-d.ts new file mode 100644 index 00000000000..24eb4bfcbf1 --- /dev/null +++ b/test-dts/apiCreateApp.test-d.ts @@ -0,0 +1,42 @@ +import { createApp, App } from './index' + +describe('plugin params inference', () => { + const app = createApp({}) + const pluginFunction = ( + app: App, + param1: number, + param2: { foo: string[] }, + param3?: boolean + ) => {} + const pluginObject = { + install: ( + app: App, + param1: number, + param2: { foo: string[] }, + param3?: boolean + ) => {} + } + class PluginClass { + static install( + app: App, + param1: number, + param2: { foo: string[] }, + param3?: boolean + ) {} + } + + // @ts-expect-error + app.use(pluginFunction, 123, { foo: [123] }) + // no error + app.use(pluginFunction, 123, { foo: ['asdf'] }) + + // @ts-expect-error + app.use(pluginObject, 123, { foo: [123] }) + // no error + app.use(pluginObject, 123, { foo: ['asdf'] }) + + // @ts-expect-error + app.use(PluginClass, 123, { foo: [123] }) + // no error + app.use(PluginClass, 123, { foo: ['asdf'] }) +}) From f1ed822ba91ecbf772cbebbd8682daebc2b860c8 Mon Sep 17 00:00:00 2001 From: fnlCtrl Date: Tue, 8 Dec 2020 11:33:23 +0800 Subject: [PATCH 2/2] fix: simplify test --- test-dts/apiCreateApp.test-d.ts | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/test-dts/apiCreateApp.test-d.ts b/test-dts/apiCreateApp.test-d.ts index 24eb4bfcbf1..db4cc10c689 100644 --- a/test-dts/apiCreateApp.test-d.ts +++ b/test-dts/apiCreateApp.test-d.ts @@ -4,39 +4,28 @@ describe('plugin params inference', () => { const app = createApp({}) const pluginFunction = ( app: App, - param1: number, - param2: { foo: string[] }, - param3?: boolean + param1: { foo: string[] }, + param2?: boolean ) => {} const pluginObject = { - install: ( - app: App, - param1: number, - param2: { foo: string[] }, - param3?: boolean - ) => {} + install: (app: App, param1: { foo: string[] }, param2?: boolean) => {} } class PluginClass { - static install( - app: App, - param1: number, - param2: { foo: string[] }, - param3?: boolean - ) {} + static install(app: App, param1: { foo: string[] }, param2?: boolean) {} } // @ts-expect-error - app.use(pluginFunction, 123, { foo: [123] }) + app.use(pluginFunction, { foo: [123] }) // no error - app.use(pluginFunction, 123, { foo: ['asdf'] }) + app.use(pluginFunction, { foo: ['asdf'] }) // @ts-expect-error - app.use(pluginObject, 123, { foo: [123] }) + app.use(pluginObject, { foo: [123] }) // no error - app.use(pluginObject, 123, { foo: ['asdf'] }) + app.use(pluginObject, { foo: ['asdf'] }) // @ts-expect-error - app.use(PluginClass, 123, { foo: [123] }) + app.use(PluginClass, { foo: [123] }) // no error - app.use(PluginClass, 123, { foo: ['asdf'] }) + app.use(PluginClass, { foo: ['asdf'] }) })