-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
SuggestionAn idea for TypeScriptAn idea for TypeScriptToo ComplexAn issue which adding support for may be too complex for the value it addsAn issue which adding support for may be too complex for the value it adds
Description
The current pattern for method overloading in a class is:
class Example {
// Overload signatures:
func(param1: number, param2: string): number;
func(param1: number[], param2: boolean[]): number[];
// Actual method implementation:
func(param1: number | number[], param2: string | boolean[]): number | number[] {
...
}
}
However the implementation signature is only in practice beneficial for parameter references within the body of the function. E.g., even if the parameter types of the implementation signature were, say, all set to any
:
func(param1: any, param2: any): any {
...
}
A caller is still bound by the overload signatures:
let example = new Example();
example.func("ABC", 123); // Error! no overload of 'func' matches the arguments
Suggestion:
The parameter and return type of the implementation signature could be implicitly inferred if they are not given an explicit type:
class Example {
func(param1: number, param2: string): number;
func(param1: number[], param2: boolean[]): number[];
func(param1, param2) {
let x = param1; // type of x is number | number[]
let y = param2; // type of y is string | boolean[]
return "ABC"; // Error: return type has been implicitly inferred to number | number[]
}
}
Having this would make it less cumbersome to define and maintain overloaded methods.
Has this ever been discussed before? and if so, what were the arguments against it?
Metadata
Metadata
Assignees
Labels
SuggestionAn idea for TypeScriptAn idea for TypeScriptToo ComplexAn issue which adding support for may be too complex for the value it addsAn issue which adding support for may be too complex for the value it adds