Skip to content

types: Added stricter type checking for plugin in vue use function #5760

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions packages/runtime-core/__tests__/apiCreateApp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
Plugin,
ref,
getCurrentInstance,
defineComponent
defineComponent,
App
} from '@vue/runtime-test'

describe('api: createApp', () => {
Expand Down Expand Up @@ -273,12 +274,12 @@ describe('api: createApp', () => {

test('use', () => {
const PluginA: Plugin = app => app.provide('foo', 1)
const PluginB: Plugin = {
install: (app, arg1, arg2) => app.provide('bar', arg1 + arg2)
const PluginB = {
install: (app: App, arg1: number, arg2: number) => app.provide('bar', arg1 + arg2)
}
class PluginC {
someProperty = {}
static install() {
static install(app: App) {
app.provide('baz', 2)
}
}
Expand Down
16 changes: 11 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<T extends Plugin>(plugin: T, ...options: ExtractPluginArg<T>): this
mixin(mixin: ComponentOptions): this
component(name: string): Component | undefined
component(name: string, component: Component): this
Expand Down Expand Up @@ -139,12 +139,18 @@ export interface AppContext {

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

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

export type ExtractPluginArg<T> = T extends Plugin<infer R>
? R extends (app: App, ...arg: infer S) => any
? S
: never
: never;

export function createAppContext(): AppContext {
return {
app: null as any,
Expand Down Expand Up @@ -215,7 +221,7 @@ export function createAppAPI<HostElement>(
}
},

use(plugin: Plugin, ...options: any[]) {
use<T extends Plugin>(plugin: T, ...options: ExtractPluginArg<T>) {
if (installedPlugins.has(plugin)) {
__DEV__ && warn(`Plugin has already been applied to target app.`)
} else if (plugin && isFunction(plugin.install)) {
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-core/src/compat/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
AppConfig,
AppContext,
CreateAppFunction,
ExtractPluginArg,
Plugin
} from '../apiCreateApp'
import {
Expand Down Expand Up @@ -77,7 +78,7 @@ export type CompatVue = Pick<App, 'version' | 'component' | 'directive'> & {

nextTick: typeof nextTick

use(plugin: Plugin, ...options: any[]): CompatVue
use<T extends Plugin>(plugin: T, ...options: ExtractPluginArg<T>): CompatVue
mixin(mixin: ComponentOptions): CompatVue

component(name: string): Component | undefined
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ export {
AppContext,
Plugin,
CreateAppFunction,
OptionMergeFunction
OptionMergeFunction,
ExtractPluginArg
} from './apiCreateApp'
export {
VNode,
Expand Down