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.










share|improve this question


























    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.










    share|improve this question
























      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.










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 4 at 10:12









      Sam96

      537




      537
























          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
          }
          }





          share|improve this answer





















          • 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, 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


















          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.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            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
































            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
            }
            }





            share|improve this answer





















            • 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, 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















            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
            }
            }





            share|improve this answer





















            • 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, 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













            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
            }
            }





            share|improve this answer












            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
            }
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            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, 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


















            • 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, 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
















            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












            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.






            share|improve this answer

























              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.






              share|improve this answer























                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.






                share|improve this answer












                @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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 4 at 18:11









                Sam96

                537




                537






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    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




















































































                    這個網誌中的熱門文章

                    Academy of Television Arts & Sciences

                    L'Équipe

                    1995 France bombings