-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Type Error when implementing Vue "Plugin" type in TypeScript #8577
Comments
In Vue, the Plugin interface is a union type. A class cannot implement a union type, so you can define its custom type. interface IPlugin<T = any> {
install: (app: App, options: T) => any
}
class MyPlugin implements IPlugin<Record<name,string>> {
install(app: App, options: Record<name,string> ) {}
} Alternatively, you can omit the "implements" keyword. You just need to ensure that the class implements the "install" method according to the definition of the Plugin interface. |
@Alfred-Skyblue, thanks for your comments. There is a type definition of "Plugin" in Vue types. But. it's not usable. Then, why is it there? |
Because const myPlugin: Plugin = {
install(app: App) {}
} |
Try |
@sxzz |
Vue version
3.3.4
Link to minimal reproduction
https://stackblitz.com/edit/vitejs-vite-gbgbxq?terminal=build
Steps to reproduce
Stackbliz link shows the TS compile error in plugin.ts.
Run
npm run build
This example shows a type error in vue plugin implementation.
src/plugins/plugin.ts:9:41 - error TS2422: A class can only implement an object type or intersection of object types with statically known members.
What is expected?
Vue types provide Plugin, PluginInstallFunction and others for plugin implementation. It should be properly used for implementation of plugins without Type Error as above.
I would like to ask you to consider revising Type definitions for Plugin.
What is actually happening?
This vue plugin is a class implementation which has 'install' function, which comply with a general guidance of vue plugin implementation.
In a sense of strict type manner for vue plugin implementation, this example tried to "implements" vue Plugin type to the class. But, the Plugin type is not working with class's "implements" directive of TypeScript, since the type is likely dynamic and undetermined for the target class or so on. Thus, we see the following error when compiling
*.ts
.src/plugins/plugin.ts:9:41 - error TS2422: A class can only implement an object type or intersection of object types with statically known members.
System Info
Any additional comments?
Thanks in advance for your taking a look for this issue!
The text was updated successfully, but these errors were encountered: