-
Notifications
You must be signed in to change notification settings - Fork 12.8k
keyof ignores constraint #44985
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
Comments
Having |
So, does that mean it is currently not possible to do? |
The type |
This works without issue type Listeners<Events, K extends keyof Events> = Record<K, Listener<Events[K]>>; |
Thanks guys, Lastly, do you know why there is the last error at the
it seems to me that those types should be the same, no? One is on<Type extends keyof Events>(type: Type, listener: Listener<Events[Type]>): Unsubscribe {
if (!type) throw new Error('Cannot call <EventEmitter>.on without a type');
if (!listener) throw new Error('Cannot call <EventEmitter>.on without a listener');
this.#listeners[type] = this.#listeners[type] || [];
this.#listeners[type].push(listener);
return () => {
this.off(type, listener);
}
} |
Looks like I had a mistake in the mapped type, working version for anyone interested Thanks @MartinJohns and @RyanCavanaugh |
Bug Report
π Search Terms
keyof, generic constraint
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
I am getting ts errors even though the types work as expected. See // usage section
π Expected behavior
The keyof returns the same type as the constraint generic and thus will be valid as index signature.
The text was updated successfully, but these errors were encountered: