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

Extended TypeScript support #41

Merged
merged 3 commits into from
Feb 18, 2021
Merged

Extended TypeScript support #41

merged 3 commits into from
Feb 18, 2021

Conversation

malj
Copy link
Contributor

@malj malj commented May 3, 2020

Extended TypeScript support which provides optional event typing and fixes some minor issues:

  • full type support for string event names, including listener argument types
  • partial type support for "*": available event names are typed, but listener's data argument type can't be inferred accurately in TypeScript yet (Generics extending unions cannot be narrowed microsoft/TypeScript#13995)
  • basic type support for symbol event names: symbols can't be narrowed down to literal types nor used as indexers in TypeScript (Allow indexing with symbols microsoft/TypeScript#1863)
  • changed private properties' access level to protected to enable access when inheriting, in case anyone wanted to expand Nanobus functionality
  • added typed _emit() method
  • fixed _starListeners property name typo
type Events = {
    a: (n: number) => void
    b: (n: number, s: string) => void
}

const emitter = new Nanobus<Events>()

emitter.emit("a", 0) // required arguments: [number]
emitter.on("a", n => {
    // n: number
})

emitter.emit("b", 0, "") // required arguments: [number, string]
emitter.on("b", (n, s) => {
    // n: number
    // s: string
})

const symbol = Symbol()
emitter.emit(symbol)  // arguments: any[]
emitter.on(symbol, (...args) => {
    // args: any[]
})

emitter.on("*", (eventName, data, uuid) => {
    // eventName: "a" | "b" | symbol
    // data: any[]
    // uuid: string | undefined
})

- full type support for string event names
- partial type support for star events (only event names)
- basic type support for symbol event names
- private property access level changed to protected (for inheritance)
@malj
Copy link
Contributor Author

malj commented May 3, 2020

Just noticed the other pull request, I apologize for skipping it. I went over its code, this is the same concept with additional symbol names and inheritance support, and fixed removeAllListeners() method signature. Is there a reason why it wasn't merged back in October?

@Tyler-Murphy
Copy link

I wrote that other pull request. I forgot about it. I don't think there was a good reason not to merge it, other than support for symbols.

This looks great! I'll close my PR in favor of this one.

With microsoft/TypeScript#38234, which is in the Typescript 4.0 beta, it might be possible to replace the values in type Events with tuples, but I haven't looked into it, and depending how soon it's out of beta, it might not be worth waiting.

@malj
Copy link
Contributor Author

malj commented Aug 2, 2020

Hi @Tyler-Murphy, thanks for the update. I apologize again for missing your PR, I would have forked and updated it if I had noticed it earlier.
Using tuples is possible since TypeScript 3.0 even without named members from the PR you linked. I had considered using them for the Events config type instead of function signatures, but decided in favor of the more explicit API.

Copy link
Member

@goto-bus-stop goto-bus-stop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thank you both for the PRs 😊

@goto-bus-stop goto-bus-stop merged commit e1d5f8c into choojs:master Feb 18, 2021
@goto-bus-stop
Copy link
Member

📦 4.5.0

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.

3 participants