Skip to content

Commit f6f64be

Browse files
authored
types: enhance plugin type inference for better IDE support (#13063)
* types: enhance plugin type inference for better JSDoc and IDE support * test: clean up * chore: tweaks
1 parent 10e54dc commit f6f64be

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

packages-private/dts-test/appUse.test-d.ts

+38-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ app.use(PluginWithoutType, 2)
1212
app.use(PluginWithoutType, { anything: 'goes' }, true)
1313

1414
type PluginOptions = {
15+
/** option1 */
1516
option1?: string
17+
/** option2 */
1618
option2: number
19+
/** option3 */
1720
option3: boolean
1821
}
1922

@@ -25,6 +28,20 @@ const PluginWithObjectOptions = {
2528
},
2629
}
2730

31+
const objectPluginOptional = {
32+
install(app: App, options?: PluginOptions) {},
33+
}
34+
app.use(objectPluginOptional)
35+
app.use(
36+
objectPluginOptional,
37+
// Test JSDoc and `go to definition` for options
38+
{
39+
option1: 'foo',
40+
option2: 1,
41+
option3: true,
42+
},
43+
)
44+
2845
for (const Plugin of [
2946
PluginWithObjectOptions,
3047
PluginWithObjectOptions.install,
@@ -92,7 +109,27 @@ const PluginTyped: Plugin<PluginOptions> = (app, options) => {}
92109

93110
// @ts-expect-error: needs options
94111
app.use(PluginTyped)
95-
app.use(PluginTyped, { option2: 2, option3: true })
112+
app.use(
113+
PluginTyped,
114+
// Test autocomplete for options
115+
{
116+
option1: '',
117+
option2: 2,
118+
option3: true,
119+
},
120+
)
121+
122+
const functionPluginOptional = (app: App, options?: PluginOptions) => {}
123+
app.use(functionPluginOptional)
124+
app.use(functionPluginOptional, { option2: 2, option3: true })
125+
126+
// type optional params
127+
const functionPluginOptional2: Plugin<[options?: PluginOptions]> = (
128+
app,
129+
options,
130+
) => {}
131+
app.use(functionPluginOptional2)
132+
app.use(functionPluginOptional2, { option2: 2, option3: true })
96133

97134
// vuetify usage
98135
const key: string = ''

packages/runtime-core/src/apiCreateApp.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ export interface App<HostElement = any> {
3636

3737
use<Options extends unknown[]>(
3838
plugin: Plugin<Options>,
39-
...options: Options
39+
...options: NoInfer<Options>
4040
): this
41-
use<Options>(plugin: Plugin<Options>, options: Options): this
41+
use<Options>(plugin: Plugin<Options>, options: NoInfer<Options>): this
4242

4343
mixin(mixin: ComponentOptions): this
4444
component(name: string): Component | undefined
@@ -215,9 +215,11 @@ export type ObjectPlugin<Options = any[]> = {
215215
export type FunctionPlugin<Options = any[]> = PluginInstallFunction<Options> &
216216
Partial<ObjectPlugin<Options>>
217217

218-
export type Plugin<Options = any[]> =
219-
| FunctionPlugin<Options>
220-
| ObjectPlugin<Options>
218+
export type Plugin<
219+
Options = any[],
220+
// TODO: in next major Options extends unknown[] and remove P
221+
P extends unknown[] = Options extends unknown[] ? Options : [Options],
222+
> = FunctionPlugin<P> | ObjectPlugin<P>
221223

222224
export function createAppContext(): AppContext {
223225
return {

0 commit comments

Comments
 (0)