Closed as not planned
Description
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:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.