Search Terms
parameter type interface for overloaded functions as union type
Suggestion
The following method:
/**
* Obtain the parameters of a function type in a tuple
*/
type Parameters<T extends (...args: any[]) => any> = T extends (...args: infer P) => any ? P : never;
Could return an union type when used with overloaded methods instead of the last overload
Use Cases
Message event name safety defined by overloaded signatures
Workaround is to use an enum instead if working in a typescript context.
Examples
export interface Emitter {
emit(event: 'event_1'): void;
emit(event: 'event_2'): void;
emit(event: 'event_3'): void;
emit(event: 'event_4'): void;
}
type EventName = Parameters<Emitter["emit"]>[0]
// is -> type EventName = "event_4"
// wanted -> type EventName = "event_1" | "event_2" | "event_3" | "event_4"
const a: EventName = "event_4";
const b: EventName = "event_1";
// error, because -> const b: "event_4"
Checklist
My suggestion meets these guidelines:
Search Terms
parameter type interface for overloaded functions as union type
Suggestion
The following method:
Could return an union type when used with overloaded methods instead of the last overload
Use Cases
Message event name safety defined by overloaded signatures
Workaround is to use an enum instead if working in a typescript context.
Examples
Checklist
My suggestion meets these guidelines: