-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion: implicitly infer parameter types for the implementation signature of an overloaded method #7763
Comments
Strangely I don't think anyone's ever proposed this. It makes a lot of sense. Just to write out the rules:
|
That's a good idea, given that it's what most people end up writing today. The downside of that is that you're still stuck with a union of every member, so you'd need to continue casting or doing type checks, whereas usually in JavaScript you discriminate your overloads based on some of the arguments you were given. Ideally, we'd be able to actually tie the uses of a variable to its potential overload as well. For instance, if I had function drawLine(startX: number, startY: number, endX: number, endY: number): void;
function drawLine(start: { x: number; y: number }, end: { x: number; y: number }): void;
function drawLine(a, b, c, d) {
if (typeof a === "number") {
// 'a', 'b', 'c', and 'd' have type 'number' here.
}
else {
// 'a' and 'b' have type '{ x: number; y: number }.
// 'c' and 'd have type 'undefined'.
}
} |
Concern from the slog was that this could potentially introduce some very unwieldy types by default. Implementors may prefer |
The current pattern for method overloading in a class is:
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
:A caller is still bound by the overload signatures:
Suggestion:
The parameter and return type of the implementation signature could be implicitly inferred if they are not given an explicit type:
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?
The text was updated successfully, but these errors were encountered: