Created
December 12, 2016 04:20
-
-
Save wonderful-panda/52aabaa288e9aefa43860baba8514663 to your computer and use it in GitHub Desktop.
Vue: コンポーネントのemitを型付けする(できてない)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // in vue.d.ts | |
| declare class Vue { | |
| $emit(event: string, ...args: any[]): this; | |
| } | |
| // in user code | |
| interface Emit<T> { | |
| <K extends keyof T>(event: K, payload: T[K]): any; | |
| } | |
| interface FooEvents { | |
| activated: {}, | |
| changed: { source: string } | |
| } | |
| // できない | |
| // TS2424: Class 'Vue' defines instance member function '$emit', but extended class 'Foo' defines it as instance member property. | |
| class Foo extends Vue { | |
| $emit: Emit<FooEvents>; // eventとして activated, changedだけを受け入れ、payloadの型もnameに応じてチェックされる | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment