aboutsummaryrefslogtreecommitdiff
path: root/test/test-types-compilation.ts
blob: 00510dabeabe42d9eb802b9f1a4cf6257c306cf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-unused-vars */

import mitt from '..';

const emitter = mitt();

/*
 * Check that if on is provided a generic, it only accepts handlers of that type
 */
{
	const badHandler = (x: number) => {};
	const goodHandler = (x: string) => {};

	// @ts-expect-error
	emitter.on<string>('foo', badHandler);
	emitter.on<string>('foo', goodHandler);
}

/*
 * Check that if off is provided a generic, it only accepts handlers of that type
 */
{
	const badHandler = (x: number) => {};
	const goodHandler = (x: string) => {};

	// @ts-expect-error
	emitter.off<string>('foo', badHandler);
	emitter.off<string>('foo', goodHandler);
}


/*
 * Check that if emitt is provided a generic, it only accepts event data of that type
 */
{
	interface SomeEventData {
    name: string;
  }
  // @ts-expect-error
	emitter.emit<SomeEventData>('foo', 'NOT VALID');
	emitter.emit<SomeEventData>('foo', { name: 'jack' });
}