diff options
| author | Jason Miller <[email protected]> | 2021-06-23 10:45:19 -0400 |
|---|---|---|
| committer | Jason Miller <[email protected]> | 2021-06-23 10:45:19 -0400 |
| commit | c580f81a4c5fd99a01c5529427e0858abca63de3 (patch) | |
| tree | 75d1d48d03d663c07236f9442c4076b373f911a6 | |
| parent | 0cce8fa09eb7f7d359c001a9b711c3369372dcaf (diff) | |
| parent | 9539abe200172cf98eae41c8da835d89a8c8da4c (diff) | |
Merge branch 'andreiTn-patch-1'
| -rw-r--r-- | README.md | 25 |
1 files changed, 21 insertions, 4 deletions
@@ -86,11 +86,28 @@ Set `"strict": true` in your tsconfig.json to get improved type inference for `m import mitt from 'mitt'; type Events = { - foo: string - bar?: number -} + foo: string; + bar?: number; +}; -const emitter: mitt.Emitter<Events> = mitt<Events>(); +const emitter = mitt<Events>(); // inferred as Emitter<Events> + +emitter.on('foo', (e) => {}); // 'e' has inferred type 'string' + +emitter.emit('foo', 42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345) +``` + +Alternatively, you can use the provided `Emitter` type: + +```ts +import mitt, { Emitter } from 'mitt'; + +type Events = { + foo: string; + bar?: number; +}; + +const emitter: Emitter<Events> = mitt<Events>(); ``` ## Examples & Demos |
