From bce1136f5d08009dcdcab94cea9941b5bea568b5 Mon Sep 17 00:00:00 2001 From: Huanyi Xie <74809554+xiehuanyi@users.noreply.github.com> Date: Sun, 11 Oct 2026 15:13:15 +0300 Subject: [PATCH] fix: allow removing all wildcard handlers in types --- src/index.ts | 2 +- test/index_test.ts | 15 +++++++++++++++ test/test-types-compilation.ts | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 17672aa..149d932 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,7 +30,7 @@ export interface Emitter> { type: Key, handler?: Handler ): void; - off(type: '*', handler: WildcardHandler): void; + off(type: '*', handler?: WildcardHandler): void; emit(type: Key, event: Events[Key]): void; emit( diff --git a/test/index_test.ts b/test/index_test.ts index 31c9495..5a57ace 100644 --- a/test/index_test.ts +++ b/test/index_test.ts @@ -157,6 +157,21 @@ describe('mitt#', () => { }); }); + it('off("*") should remove all wildcard handlers only', () => { + const wildcard = spy(); + const foo = spy(); + inst.on('*', wildcard); + inst.on('*', spy()); + inst.on('foo', foo); + + inst.off('*'); + inst.emit('foo', 'value'); + + expect(events.get('*')).to.deep.equal([]); + expect(wildcard).not.to.have.been.called; + expect(foo).to.have.been.calledOnce.and.calledWith('value'); + }); + describe('emit()', () => { it('should be a function', () => { expect(inst).to.have.property('emit').that.is.a('function'); diff --git a/test/test-types-compilation.ts b/test/test-types-compilation.ts index 476e631..01db153 100644 --- a/test/test-types-compilation.ts +++ b/test/test-types-compilation.ts @@ -50,6 +50,8 @@ const wildcardHandler = ( // @ts-expect-error emitter.off('bar', fooHandler); + emitter.off('*'); + emitter.off('*', undefined); emitter.off('*', wildcardHandler); // fooHandler is ok, because ('foo' | 'bar' | 'someEvent') extends string emitter.off('*', fooHandler);