Parameter type interface for overloaded functions as union type
See original GitHub issueSearch 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.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:92
- Comments:28 (3 by maintainers)
Top Results From Across the Web
Overloaded method of an interface cannot accept a union type ...
The method can accept either null or string value as a parameter. I define a union type: string | null and pass it...
Read more >Overloaded functions in TypeScript - Damir's Corner
In this post, I presented an example of how function overload signatures, union types, and custom type guards can be used to tell...
Read more >Type polymorphic functions in TypeScript - Zhenghao
The argument's type could either be a string or boolean , so we use a union type to model this. Then, we use...
Read more >Documentation - Advanced Types - TypeScript
A predicate takes the form parameterName is Type , where parameterName must be the name of a parameter from the current function signature....
Read more >Overload Functions to Enrich your Definition - Learn TypeScript
The advantage of overloaded functions is clarity for the consumer. A consumer sees several functions with different signatures (parameters and return types).
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The case here though was
infer
over the entire parameter list which in the case above would yield[string,string] | [number,number]
—which precisely describes the valid inputs tofn
.It’s not perfect but you can tease some info out of the compiler about overloads… up to an arbitrary fixed number of overloads, modulo some weird bugs with zero-arg function types (#28867)
Click to expand
Playground link