Skip to content

Instantly share code, notes, and snippets.

@wonderful-panda
Created December 12, 2016 04:20
Show Gist options
  • Select an option

  • Save wonderful-panda/52aabaa288e9aefa43860baba8514663 to your computer and use it in GitHub Desktop.

Select an option

Save wonderful-panda/52aabaa288e9aefa43860baba8514663 to your computer and use it in GitHub Desktop.
Vue: コンポーネントのemitを型付けする(できてない)
// 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