Typescript: Overload with optional paramters: “Overload signature is not compatible with function...
up vote
1
down vote
favorite
I've been experimenting with overloads (originally trying to implement this basic proxy example in typescript without foul tricks like disabling type checking on a line, if any of you have solved that feel free to spoiler me, I have already learned much and its getting annoying that I cant seem to solve it).
I've been running into an problem while experimenting. Here's my code:
// "Overload signature is not compatible with function implementation." on first (in code) declaration
function strOrObj (samePar:object, obj:object):object|undefined;
function strOrObj (samePar:object, num:number):number|undefined;
function strOrObj (samePar:object, num?:number, obj?:object):object|number|undefined{
console.log("obj="+obj, "num="+num);
return obj? obj: num;
}
I had to add the undefined on the implementation because the compiler, looking at the implementation, thought it might be needed. It didnt ask for me to add this to the overrides, but I did it anyways to experiment. But when I add type declarations in the implementation I get the error Overload signature is not compatible with function implementation..
The only way I get this to compile is to declare any parameter in the implementation declaration as any, but that way it'll just stuff every given type into the first optional parameter (num in this case).
So the queston is, what is wrong here? Is it even possible to have multiple optional parameters if youre not sure the first one will be used or is that maybe the problem (which could be a thing because of the JS implementation)?
Sorry if this is a duplicate, the only thing I could find that seemed to apply was an issue on Github, but the fixes seem to have been merged into the release code.
typescript override
add a comment |
up vote
1
down vote
favorite
I've been experimenting with overloads (originally trying to implement this basic proxy example in typescript without foul tricks like disabling type checking on a line, if any of you have solved that feel free to spoiler me, I have already learned much and its getting annoying that I cant seem to solve it).
I've been running into an problem while experimenting. Here's my code:
// "Overload signature is not compatible with function implementation." on first (in code) declaration
function strOrObj (samePar:object, obj:object):object|undefined;
function strOrObj (samePar:object, num:number):number|undefined;
function strOrObj (samePar:object, num?:number, obj?:object):object|number|undefined{
console.log("obj="+obj, "num="+num);
return obj? obj: num;
}
I had to add the undefined on the implementation because the compiler, looking at the implementation, thought it might be needed. It didnt ask for me to add this to the overrides, but I did it anyways to experiment. But when I add type declarations in the implementation I get the error Overload signature is not compatible with function implementation..
The only way I get this to compile is to declare any parameter in the implementation declaration as any, but that way it'll just stuff every given type into the first optional parameter (num in this case).
So the queston is, what is wrong here? Is it even possible to have multiple optional parameters if youre not sure the first one will be used or is that maybe the problem (which could be a thing because of the JS implementation)?
Sorry if this is a duplicate, the only thing I could find that seemed to apply was an issue on Github, but the fixes seem to have been merged into the release code.
typescript override
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I've been experimenting with overloads (originally trying to implement this basic proxy example in typescript without foul tricks like disabling type checking on a line, if any of you have solved that feel free to spoiler me, I have already learned much and its getting annoying that I cant seem to solve it).
I've been running into an problem while experimenting. Here's my code:
// "Overload signature is not compatible with function implementation." on first (in code) declaration
function strOrObj (samePar:object, obj:object):object|undefined;
function strOrObj (samePar:object, num:number):number|undefined;
function strOrObj (samePar:object, num?:number, obj?:object):object|number|undefined{
console.log("obj="+obj, "num="+num);
return obj? obj: num;
}
I had to add the undefined on the implementation because the compiler, looking at the implementation, thought it might be needed. It didnt ask for me to add this to the overrides, but I did it anyways to experiment. But when I add type declarations in the implementation I get the error Overload signature is not compatible with function implementation..
The only way I get this to compile is to declare any parameter in the implementation declaration as any, but that way it'll just stuff every given type into the first optional parameter (num in this case).
So the queston is, what is wrong here? Is it even possible to have multiple optional parameters if youre not sure the first one will be used or is that maybe the problem (which could be a thing because of the JS implementation)?
Sorry if this is a duplicate, the only thing I could find that seemed to apply was an issue on Github, but the fixes seem to have been merged into the release code.
typescript override
I've been experimenting with overloads (originally trying to implement this basic proxy example in typescript without foul tricks like disabling type checking on a line, if any of you have solved that feel free to spoiler me, I have already learned much and its getting annoying that I cant seem to solve it).
I've been running into an problem while experimenting. Here's my code:
// "Overload signature is not compatible with function implementation." on first (in code) declaration
function strOrObj (samePar:object, obj:object):object|undefined;
function strOrObj (samePar:object, num:number):number|undefined;
function strOrObj (samePar:object, num?:number, obj?:object):object|number|undefined{
console.log("obj="+obj, "num="+num);
return obj? obj: num;
}
I had to add the undefined on the implementation because the compiler, looking at the implementation, thought it might be needed. It didnt ask for me to add this to the overrides, but I did it anyways to experiment. But when I add type declarations in the implementation I get the error Overload signature is not compatible with function implementation..
The only way I get this to compile is to declare any parameter in the implementation declaration as any, but that way it'll just stuff every given type into the first optional parameter (num in this case).
So the queston is, what is wrong here? Is it even possible to have multiple optional parameters if youre not sure the first one will be used or is that maybe the problem (which could be a thing because of the JS implementation)?
Sorry if this is a duplicate, the only thing I could find that seemed to apply was an issue on Github, but the fixes seem to have been merged into the release code.
typescript override
typescript override
asked Nov 4 at 10:12
Sam96
537
537
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
Typescript allows you to define multiple overloads but it's your job in the implentation to diferentiate between them, this is why the implentation signature must be compatible with all overloads
function strOrObj (samePar:object, obj:object):object|undefined;
function strOrObj (samePar:object, num:number):number|undefined;
function strOrObj (samePar:object, numObj:number| object):object|number|undefined{
if (typeof numObj === 'number') {
return samePar
} else {
numObj
}
}
Yes that works, I wanted them in different variables though if possible. That makes the handling easier, I don't need to do a type test. The question is if it's possible.
– Sam96
Nov 4 at 12:04
1
@Sam96 no Typescript does not offer any support for this. The above version, using the union type, notany, is the closest you will get
– Titian Cernicova-Dragomir
Nov 4 at 13:06
A shame, but good to know. Thanks!
– Sam96
Nov 4 at 16:21
add a comment |
up vote
0
down vote
accepted
@titian-cernicova-dragomir answered my actual answer in the comments of his answer. Here I've written down what I concluded from his answer and my own testing.
Typescript doesnt allow for the name-choosing of given parameters based on their type (without explicitly writing the type check yourself). The only logic overloads implement in Typescript (at least as of now) is a check whether the overloads fit with the implementation in input and return types, and the choice of the fitting override to determine the return type.
The optional variables just get filled from right to left, disregarding the given type. Thats whats causing the error, as even a secondary object parameter ("obj") will end up in the num variable in the implementation and the compiler complains that the second parameter will never be number, thus that overload signature is invalid.
You can test that by switching the parameters, if you define the obj first in the implementation it'll complain about the 2nd overload signature, as the 2nd parameter will always be an object.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Typescript allows you to define multiple overloads but it's your job in the implentation to diferentiate between them, this is why the implentation signature must be compatible with all overloads
function strOrObj (samePar:object, obj:object):object|undefined;
function strOrObj (samePar:object, num:number):number|undefined;
function strOrObj (samePar:object, numObj:number| object):object|number|undefined{
if (typeof numObj === 'number') {
return samePar
} else {
numObj
}
}
Yes that works, I wanted them in different variables though if possible. That makes the handling easier, I don't need to do a type test. The question is if it's possible.
– Sam96
Nov 4 at 12:04
1
@Sam96 no Typescript does not offer any support for this. The above version, using the union type, notany, is the closest you will get
– Titian Cernicova-Dragomir
Nov 4 at 13:06
A shame, but good to know. Thanks!
– Sam96
Nov 4 at 16:21
add a comment |
up vote
1
down vote
Typescript allows you to define multiple overloads but it's your job in the implentation to diferentiate between them, this is why the implentation signature must be compatible with all overloads
function strOrObj (samePar:object, obj:object):object|undefined;
function strOrObj (samePar:object, num:number):number|undefined;
function strOrObj (samePar:object, numObj:number| object):object|number|undefined{
if (typeof numObj === 'number') {
return samePar
} else {
numObj
}
}
Yes that works, I wanted them in different variables though if possible. That makes the handling easier, I don't need to do a type test. The question is if it's possible.
– Sam96
Nov 4 at 12:04
1
@Sam96 no Typescript does not offer any support for this. The above version, using the union type, notany, is the closest you will get
– Titian Cernicova-Dragomir
Nov 4 at 13:06
A shame, but good to know. Thanks!
– Sam96
Nov 4 at 16:21
add a comment |
up vote
1
down vote
up vote
1
down vote
Typescript allows you to define multiple overloads but it's your job in the implentation to diferentiate between them, this is why the implentation signature must be compatible with all overloads
function strOrObj (samePar:object, obj:object):object|undefined;
function strOrObj (samePar:object, num:number):number|undefined;
function strOrObj (samePar:object, numObj:number| object):object|number|undefined{
if (typeof numObj === 'number') {
return samePar
} else {
numObj
}
}
Typescript allows you to define multiple overloads but it's your job in the implentation to diferentiate between them, this is why the implentation signature must be compatible with all overloads
function strOrObj (samePar:object, obj:object):object|undefined;
function strOrObj (samePar:object, num:number):number|undefined;
function strOrObj (samePar:object, numObj:number| object):object|number|undefined{
if (typeof numObj === 'number') {
return samePar
} else {
numObj
}
}
answered Nov 4 at 11:43
Titian Cernicova-Dragomir
49.9k33147
49.9k33147
Yes that works, I wanted them in different variables though if possible. That makes the handling easier, I don't need to do a type test. The question is if it's possible.
– Sam96
Nov 4 at 12:04
1
@Sam96 no Typescript does not offer any support for this. The above version, using the union type, notany, is the closest you will get
– Titian Cernicova-Dragomir
Nov 4 at 13:06
A shame, but good to know. Thanks!
– Sam96
Nov 4 at 16:21
add a comment |
Yes that works, I wanted them in different variables though if possible. That makes the handling easier, I don't need to do a type test. The question is if it's possible.
– Sam96
Nov 4 at 12:04
1
@Sam96 no Typescript does not offer any support for this. The above version, using the union type, notany, is the closest you will get
– Titian Cernicova-Dragomir
Nov 4 at 13:06
A shame, but good to know. Thanks!
– Sam96
Nov 4 at 16:21
Yes that works, I wanted them in different variables though if possible. That makes the handling easier, I don't need to do a type test. The question is if it's possible.
– Sam96
Nov 4 at 12:04
Yes that works, I wanted them in different variables though if possible. That makes the handling easier, I don't need to do a type test. The question is if it's possible.
– Sam96
Nov 4 at 12:04
1
1
@Sam96 no Typescript does not offer any support for this. The above version, using the union type, not
any, is the closest you will get– Titian Cernicova-Dragomir
Nov 4 at 13:06
@Sam96 no Typescript does not offer any support for this. The above version, using the union type, not
any, is the closest you will get– Titian Cernicova-Dragomir
Nov 4 at 13:06
A shame, but good to know. Thanks!
– Sam96
Nov 4 at 16:21
A shame, but good to know. Thanks!
– Sam96
Nov 4 at 16:21
add a comment |
up vote
0
down vote
accepted
@titian-cernicova-dragomir answered my actual answer in the comments of his answer. Here I've written down what I concluded from his answer and my own testing.
Typescript doesnt allow for the name-choosing of given parameters based on their type (without explicitly writing the type check yourself). The only logic overloads implement in Typescript (at least as of now) is a check whether the overloads fit with the implementation in input and return types, and the choice of the fitting override to determine the return type.
The optional variables just get filled from right to left, disregarding the given type. Thats whats causing the error, as even a secondary object parameter ("obj") will end up in the num variable in the implementation and the compiler complains that the second parameter will never be number, thus that overload signature is invalid.
You can test that by switching the parameters, if you define the obj first in the implementation it'll complain about the 2nd overload signature, as the 2nd parameter will always be an object.
add a comment |
up vote
0
down vote
accepted
@titian-cernicova-dragomir answered my actual answer in the comments of his answer. Here I've written down what I concluded from his answer and my own testing.
Typescript doesnt allow for the name-choosing of given parameters based on their type (without explicitly writing the type check yourself). The only logic overloads implement in Typescript (at least as of now) is a check whether the overloads fit with the implementation in input and return types, and the choice of the fitting override to determine the return type.
The optional variables just get filled from right to left, disregarding the given type. Thats whats causing the error, as even a secondary object parameter ("obj") will end up in the num variable in the implementation and the compiler complains that the second parameter will never be number, thus that overload signature is invalid.
You can test that by switching the parameters, if you define the obj first in the implementation it'll complain about the 2nd overload signature, as the 2nd parameter will always be an object.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
@titian-cernicova-dragomir answered my actual answer in the comments of his answer. Here I've written down what I concluded from his answer and my own testing.
Typescript doesnt allow for the name-choosing of given parameters based on their type (without explicitly writing the type check yourself). The only logic overloads implement in Typescript (at least as of now) is a check whether the overloads fit with the implementation in input and return types, and the choice of the fitting override to determine the return type.
The optional variables just get filled from right to left, disregarding the given type. Thats whats causing the error, as even a secondary object parameter ("obj") will end up in the num variable in the implementation and the compiler complains that the second parameter will never be number, thus that overload signature is invalid.
You can test that by switching the parameters, if you define the obj first in the implementation it'll complain about the 2nd overload signature, as the 2nd parameter will always be an object.
@titian-cernicova-dragomir answered my actual answer in the comments of his answer. Here I've written down what I concluded from his answer and my own testing.
Typescript doesnt allow for the name-choosing of given parameters based on their type (without explicitly writing the type check yourself). The only logic overloads implement in Typescript (at least as of now) is a check whether the overloads fit with the implementation in input and return types, and the choice of the fitting override to determine the return type.
The optional variables just get filled from right to left, disregarding the given type. Thats whats causing the error, as even a secondary object parameter ("obj") will end up in the num variable in the implementation and the compiler complains that the second parameter will never be number, thus that overload signature is invalid.
You can test that by switching the parameters, if you define the obj first in the implementation it'll complain about the 2nd overload signature, as the 2nd parameter will always be an object.
answered Nov 4 at 18:11
Sam96
537
537
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139685%2ftypescript-overload-with-optional-paramters-overload-signature-is-not-compati%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password