Skip to content

Commit

Permalink
docs: improve custom event typing
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Sep 2, 2022
1 parent ee2d017 commit 9fbfe17
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
17 changes: 13 additions & 4 deletions docs/guide/api-plugin.md
Expand Up @@ -595,12 +595,21 @@ It is possible to type custom events by extending the `CustomEventMap` interface
```ts
// events.d.ts
import 'vite'
import 'vite/client/types'

interface MyCustomEventMap {
'custom:foo': { msg: string }
// 'event-key': payload
}

// extend interface for server-side
declare module 'vite' {
interface CustomEventMap extends MyCustomEventMap {}
}

// extend interface for client-side
declare module 'vite/client/types' {
interface CustomEventMap {
'custom:foo': { msg: string }
// 'event-key': payload
}
interface CustomEventMap extends MyCustomEventMap {}
}
```
17 changes: 12 additions & 5 deletions playground/hmr/event.d.ts
@@ -1,9 +1,16 @@
import 'vite'
import 'vite/client/types'

interface MyCustomEventMap {
'custom:foo': { msg: string }
'custom:remote-add': { a: number; b: number }
'custom:remote-add-result': { result: string }
}

declare module 'vite' {
interface CustomEventMap extends MyCustomEventMap {}
}

declare module 'vite/client/types' {
interface CustomEventMap {
'custom:foo': { msg: string }
'custom:remote-add': { a: number; b: number }
'custom:remote-add-result': { result: string }
}
interface CustomEventMap extends MyCustomEventMap {}
}

0 comments on commit 9fbfe17

Please sign in to comment.