Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stronger typing #114

Merged
merged 6 commits into from Jun 22, 2021
Merged

Stronger typing #114

merged 6 commits into from Jun 22, 2021

Conversation

iyegoroff
Copy link
Contributor

This PR improves type inference for instance methods when "strict": true is present in tsconfig.json. Compiled js code isn't affected by this change, only typings.

const emitter = mitt<{
  foo: string;
  bar?: number;
}>()

// Error: Type 'string' is not assignable to type 'number'.ts(2769)
emitter.on('foo', (x: number) => {})
// Ok
emitter.on('foo', (x: string) => {})

//Error: Type 'number | undefined' is not assignable to type 'number'.
//          Type 'undefined' is not assignable to type 'number'.ts(2769)
emitter.on('bar', (x: number) => {})
// Ok
emitter.on('bar', (x?: number) => {})

// Error: Argument of type '"foo"' is not assignable to parameter of type 'never'.ts(2345)
emitter.emit('foo')
// Ok
emitter.emit('bar')

// Error: Argument of type '123' is not assignable to parameter of type 'string'.ts(2345)
emitter.emit('foo', 123)
// Ok
emitter.emit('foo', 'string')
// Ok
emitter.emit('bar', 123)

fixes #112
fixes #106

@deskoh
Copy link

deskoh commented Aug 6, 2020

I suppose this could also leverage on module augmentation / declaration merging to allow typings for different events to be declared across different files as well? See #97 (comment)

@iyegoroff
Copy link
Contributor Author

Hi @developit, could you please give some feedback on this PR?

@yoyoys
Copy link

yoyoys commented Mar 11, 2021

HI @developit, could you merge this pull request?

@iyegoroff
Copy link
Contributor Author

Since this repo looks abandoned, I put my fork to npm as strict-mitt

@y-nk
Copy link

y-nk commented May 30, 2021

@iyegoroff is the repo abandoned or just maybe @developit is super busy atm ?

@developit
Copy link
Owner

@y-nk the latter - getting this stuff landed and doing some repo maintenance just bubbled up to the top of my to-do list though.

@y-nk
Copy link

y-nk commented Jun 1, 2021

Thanks a lot @developit ! If you need any help/assitance let me know i'd be glad to help.

@developit
Copy link
Owner

Alright, I had a chance to play around with this and it's pretty great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix TypeScript type of Handler Strongly typed event names & values
5 participants