-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
QuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code
Description
Bug Report
π Search Terms
keyof, generic constraint
π Version & Regression Information
- This is a crash β No
- This changed between versions _______ and _______
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about keyof
- I was unable to test this on prior versions because it seems to be consistent behavior in all tested versions
β― Playground Link
Playground link with relevant code
π» Code
// Implementation of event emitter - sample without all methods
type Listener<Event> = (event: Event) => void;
// Here the "key: K" complains "An index signature parameter type must be either 'string' or 'number'"
type Listeners<Events, K extends string & keyof Events> = {[key: K]: Listener<Events[K]>[]}
class EventEmitter<Events extends {[key: string]: unknown}> {
#listeners: Listeners<Events, keyof Events>
constructor() {
this.#listeners = {}
}
}
// Usage
type MyEvents = {
connect: void
reconnect: number
error: Error
disconnect: void
}
const emitter = new EventEmitter<MyEvents>()
π 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.
Metadata
Metadata
Assignees
Labels
QuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code