-
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
Overloading with/without arguments throws a compiler error #5658
Comments
please see the handbook on how to express function overloads in TypeScript: https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Functions.md#overloads |
@mhegazy, thanks for your reply. I assume you are aware I was consulting these docs before filing the issue (how would I know about overloads otherwise?). But the issue remains: you can overload functions so long you provide a strict type, but there seems to be no way of having a function that takes no parameters as part of the overloading mechanism. If I'm wrong about this. Please let me know. Thanks! |
Well you already said you were familiar with Java and C#. 😉
The following should work. restoreState(): void;
restoreState(aState: TransformerState): void
restoreState(aState?: TransformerState) {
if (aState) {
this.state = astate;
}
else {
this.restoreState(this.stateStack[stateStack.length - 1]);
}
} But using more canonical TypeScript, you can get rid of the overloads altogether and just give your parameter a default value. restoreState(aState = this.stateStack[stateStack.length - 1]) {
this.state = aState
} |
Thanks @DanielRosenwasser. It may sound odd, but I don't actually need this type overriding at the moment. I was giving this a go and noticed it doesn't work. Your solution Daniel will work. But that's not the point... My argument that seeing as overloading is supported by the compiler for functions with arguments, it should (that is, it surely will surface at some point as a requirement) also support no parameter functions. In other words, the framework should support this; it's a missing feature. |
I don't understand you argument. I clearly gave you an example of a zero-parameter overload above, so what exactly do you mean? |
Oh OK... I think I understand where the confusion is. The docs say:
This completely gives the impression that the compiler is the one calling the correct overload based on the parameters provided. So I just assumed the compiler will call the right function based on its parameters:
But this throws So it's either the compiler doesn't resolve overloads for you, or it depends on a return type? Sorry, but I'm probably biased by other languages where the code above is a valid one. |
JavaScript, and so is TypeScript, does not have a concept of function overloads like in other languages. so you definition of two JS developers use checking for parameter types, values or count to achieve a form of overloading. this is similar to what @DanielRosenwasser mentioned, and similar to the samples in the documentation. There is nothing different in this respect in TypeScript. The only thing that TypeScript adds here is the ability to describe these overloads by adding an overload set to a function implementation. again using @DanielRosenwasser's example: restoreState(): void;
restoreState(aState: TransformerState): void
restoreState(aState?: TransformerState) {
....
} It is OK to call this method with 0 or 1 arguments. it is error otherwise. it is still the implementation responsibility to do the actual "overloading": restoreState(aState?: TransformerState) {
if (aState) { // 1 argument passed
this.state = astate;
}
else { // 0 arguments passed
this.restoreState(this.stateStack[stateStack.length - 1]);
}
} hope that helps. |
Thanks @mhegazy, This is clearer now. Two points:
|
The clearness documentation maybe debatable, because while it describes it, it provides examples of how to utilise it, which your examples I am afraid to follow the documentation.
This has been discussed before in other threads here, the only way to realistically achieve this is by emitting some sort of run-time overhead, which is very much against the design goals of TypeScript. In particular:
As well as the non-goal of:
|
Thanks @kitsonk. That makes sense. One thought though:
That's very reasonable, but note that if one is after function overloads one has to follow the documentation example (the router function), which are exactly the overhead the framework is trying not to introduce. So in a way, anyone who wishes to use the overloads feature will introduce the same overhead that the framework would otherwise introduce. So the whole thing is possibly half-baked? I suspect you say:
Obviously client code will be more prone to errors than framework code. If it would be up to me, I would either not provide it at all, or provide the full mechanism asserting it introduces a runtime overhead. But wouldn't leave it in a state where in order to be used a manual introduction of overload is necessary. Anyhow, as you said, this has been discussed before, and to be frank is not a make-or-break feature. So thanks everyone for their comments. |
Given the following code:
And the following call:
The compiler complains:
error TS2346: Supplied parameters do not match any signature of call target.
Language like C++ or JAVA support no argument function overloads; typescript seems not to.
The text was updated successfully, but these errors were encountered: