aboutsummaryrefslogtreecommitdiff
path: root/test/test-types-compilation.ts
diff options
context:
space:
mode:
authorMarin Ivanov <[email protected]>2026-01-19 02:41:01 +0200
committerMarin Ivanov <[email protected]>2026-01-19 02:41:01 +0200
commita6bdee992a0e8850b922944a943cdbd907db5e66 (patch)
treedd114f4e6f3fa7b21bf11541e128feef893743a5 /test/test-types-compilation.ts
parent1fc5ce993a1769a3d0dd5fe2fdb51726f06f804c (diff)
parentb240473b5707857ba2c6a8e6d707c28d1e39da49 (diff)
Merge tag '3.0.1' from mittHEADmaster
Diffstat (limited to 'test/test-types-compilation.ts')
-rw-r--r--test/test-types-compilation.ts78
1 files changed, 78 insertions, 0 deletions
diff --git a/test/test-types-compilation.ts b/test/test-types-compilation.ts
new file mode 100644
index 0000000..25c51cc
--- /dev/null
+++ b/test/test-types-compilation.ts
@@ -0,0 +1,78 @@
+/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-unused-vars */
+
+import nitt from '..';
+
+interface SomeEventData {
+ name: string;
+}
+
+const emitter = nitt<{
+ foo: string;
+ someEvent: SomeEventData;
+ bar?: number;
+}>();
+
+const barHandler = (x?: number) => {};
+const fooHandler = (x: string) => {};
+const wildcardHandler = (
+ _type: 'foo' | 'bar' | 'someEvent',
+ _event: string | SomeEventData | number | undefined
+) => {};
+
+/*
+ * Check that 'on' args are inferred correctly
+ */
+{
+ // @ts-expect-error
+ emitter.on('foo', barHandler);
+ emitter.on('foo', fooHandler);
+
+ emitter.on('bar', barHandler);
+ // @ts-expect-error
+ emitter.on('bar', fooHandler);
+
+ emitter.on('*', wildcardHandler);
+ // fooHandler is ok, because ('foo' | 'bar' | 'someEvent') extends string
+ emitter.on('*', fooHandler);
+ // @ts-expect-error
+ emitter.on('*', barHandler);
+}
+
+/*
+ * Check that 'off' args are inferred correctly
+ */
+{
+ // @ts-expect-error
+ emitter.off('foo', barHandler);
+ emitter.off('foo', fooHandler);
+
+ emitter.off('bar', barHandler);
+ // @ts-expect-error
+ emitter.off('bar', fooHandler);
+
+ emitter.off('*', wildcardHandler);
+ // fooHandler is ok, because ('foo' | 'bar' | 'someEvent') extends string
+ emitter.off('*', fooHandler);
+ // @ts-expect-error
+ emitter.off('*', barHandler);
+}
+
+/*
+ * Check that 'emit' args are inferred correctly
+ */
+{
+ // @ts-expect-error
+ emitter.emit('someEvent', 'NOT VALID');
+ emitter.emit('someEvent', { name: 'jack' });
+
+ // @ts-expect-error
+ emitter.emit('foo');
+ // @ts-expect-error
+ emitter.emit('foo', 1);
+ emitter.emit('foo', 'string');
+
+ emitter.emit('bar');
+ emitter.emit('bar', 1);
+ // @ts-expect-error
+ emitter.emit('bar', 'string');
+}