How to extend the Error interface with generic strings in Flow?












1















When I try to extend the Error interface in Flow to make the name property mandatory, Flow doesn't recognise my generic types as strings despite explicitly describing them as strings.



When I write that:



interface CustomError<A: string, B: string> extends Error {
message: A;
name: B;
}


I get these 2 (same) errors:




  • Cannot extend Error 1 with CustomError because A [2] is incompatible with string [3] in property message.

  • Cannot extend Error 1 with CustomError because B [2] is incompatible with string [3] in property name.


What's strange is that it tells me because A [2] is incompatible when A should be described as a string right away...










share|improve this question

























  • Why do you need to change Error basic members type: message and name? What you plan to put there beside the string?

    – Alex
    Nov 22 '18 at 8:34






  • 1





    @Alex I actually tried to isolate my issue. Not only I added other properties in my original code but the goal here is to make the name, which is natively not a mandatory property, a mandatory one. As for why I need it to be generic: this allows autocompletion to "show" the related strings since there is an error dictionary in the form of a string map object linked to these generic types.

    – Edouard Hienrichs
    Nov 22 '18 at 8:43


















1















When I try to extend the Error interface in Flow to make the name property mandatory, Flow doesn't recognise my generic types as strings despite explicitly describing them as strings.



When I write that:



interface CustomError<A: string, B: string> extends Error {
message: A;
name: B;
}


I get these 2 (same) errors:




  • Cannot extend Error 1 with CustomError because A [2] is incompatible with string [3] in property message.

  • Cannot extend Error 1 with CustomError because B [2] is incompatible with string [3] in property name.


What's strange is that it tells me because A [2] is incompatible when A should be described as a string right away...










share|improve this question

























  • Why do you need to change Error basic members type: message and name? What you plan to put there beside the string?

    – Alex
    Nov 22 '18 at 8:34






  • 1





    @Alex I actually tried to isolate my issue. Not only I added other properties in my original code but the goal here is to make the name, which is natively not a mandatory property, a mandatory one. As for why I need it to be generic: this allows autocompletion to "show" the related strings since there is an error dictionary in the form of a string map object linked to these generic types.

    – Edouard Hienrichs
    Nov 22 '18 at 8:43
















1












1








1








When I try to extend the Error interface in Flow to make the name property mandatory, Flow doesn't recognise my generic types as strings despite explicitly describing them as strings.



When I write that:



interface CustomError<A: string, B: string> extends Error {
message: A;
name: B;
}


I get these 2 (same) errors:




  • Cannot extend Error 1 with CustomError because A [2] is incompatible with string [3] in property message.

  • Cannot extend Error 1 with CustomError because B [2] is incompatible with string [3] in property name.


What's strange is that it tells me because A [2] is incompatible when A should be described as a string right away...










share|improve this question
















When I try to extend the Error interface in Flow to make the name property mandatory, Flow doesn't recognise my generic types as strings despite explicitly describing them as strings.



When I write that:



interface CustomError<A: string, B: string> extends Error {
message: A;
name: B;
}


I get these 2 (same) errors:




  • Cannot extend Error 1 with CustomError because A [2] is incompatible with string [3] in property message.

  • Cannot extend Error 1 with CustomError because B [2] is incompatible with string [3] in property name.


What's strange is that it tells me because A [2] is incompatible when A should be described as a string right away...







flowtype






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 6:51







Edouard Hienrichs

















asked Nov 19 '18 at 1:53









Edouard HienrichsEdouard Hienrichs

38211




38211













  • Why do you need to change Error basic members type: message and name? What you plan to put there beside the string?

    – Alex
    Nov 22 '18 at 8:34






  • 1





    @Alex I actually tried to isolate my issue. Not only I added other properties in my original code but the goal here is to make the name, which is natively not a mandatory property, a mandatory one. As for why I need it to be generic: this allows autocompletion to "show" the related strings since there is an error dictionary in the form of a string map object linked to these generic types.

    – Edouard Hienrichs
    Nov 22 '18 at 8:43





















  • Why do you need to change Error basic members type: message and name? What you plan to put there beside the string?

    – Alex
    Nov 22 '18 at 8:34






  • 1





    @Alex I actually tried to isolate my issue. Not only I added other properties in my original code but the goal here is to make the name, which is natively not a mandatory property, a mandatory one. As for why I need it to be generic: this allows autocompletion to "show" the related strings since there is an error dictionary in the form of a string map object linked to these generic types.

    – Edouard Hienrichs
    Nov 22 '18 at 8:43



















Why do you need to change Error basic members type: message and name? What you plan to put there beside the string?

– Alex
Nov 22 '18 at 8:34





Why do you need to change Error basic members type: message and name? What you plan to put there beside the string?

– Alex
Nov 22 '18 at 8:34




1




1





@Alex I actually tried to isolate my issue. Not only I added other properties in my original code but the goal here is to make the name, which is natively not a mandatory property, a mandatory one. As for why I need it to be generic: this allows autocompletion to "show" the related strings since there is an error dictionary in the form of a string map object linked to these generic types.

– Edouard Hienrichs
Nov 22 '18 at 8:43







@Alex I actually tried to isolate my issue. Not only I added other properties in my original code but the goal here is to make the name, which is natively not a mandatory property, a mandatory one. As for why I need it to be generic: this allows autocompletion to "show" the related strings since there is an error dictionary in the form of a string map object linked to these generic types.

– Edouard Hienrichs
Nov 22 '18 at 8:43














1 Answer
1






active

oldest

votes


















1





+50









It's also not clear for me why it doesn't work, however something like this seems to be ok:



class CustomError<A: string, B: string> extends Error {

constructor(name: A, message: B) {
super(name);
this.name = name;
this.message = message;
}
}

type A = 'A' | 'A1';
type B = 'B' | 'B1';

class SpecificError extends CustomError<A, B> {}

//throw new SpecificError('a', 'c'); //error, wrong argument

throw new SpecificError('A', 'B'); //ok


Also, would note that js allows to throw any expression, not only Error children, so you might not need to extend Error class.






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',
    autoActivateHeartbeat: false,
    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%2f53367318%2fhow-to-extend-the-error-interface-with-generic-strings-in-flow%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1





    +50









    It's also not clear for me why it doesn't work, however something like this seems to be ok:



    class CustomError<A: string, B: string> extends Error {

    constructor(name: A, message: B) {
    super(name);
    this.name = name;
    this.message = message;
    }
    }

    type A = 'A' | 'A1';
    type B = 'B' | 'B1';

    class SpecificError extends CustomError<A, B> {}

    //throw new SpecificError('a', 'c'); //error, wrong argument

    throw new SpecificError('A', 'B'); //ok


    Also, would note that js allows to throw any expression, not only Error children, so you might not need to extend Error class.






    share|improve this answer




























      1





      +50









      It's also not clear for me why it doesn't work, however something like this seems to be ok:



      class CustomError<A: string, B: string> extends Error {

      constructor(name: A, message: B) {
      super(name);
      this.name = name;
      this.message = message;
      }
      }

      type A = 'A' | 'A1';
      type B = 'B' | 'B1';

      class SpecificError extends CustomError<A, B> {}

      //throw new SpecificError('a', 'c'); //error, wrong argument

      throw new SpecificError('A', 'B'); //ok


      Also, would note that js allows to throw any expression, not only Error children, so you might not need to extend Error class.






      share|improve this answer


























        1





        +50







        1





        +50



        1




        +50





        It's also not clear for me why it doesn't work, however something like this seems to be ok:



        class CustomError<A: string, B: string> extends Error {

        constructor(name: A, message: B) {
        super(name);
        this.name = name;
        this.message = message;
        }
        }

        type A = 'A' | 'A1';
        type B = 'B' | 'B1';

        class SpecificError extends CustomError<A, B> {}

        //throw new SpecificError('a', 'c'); //error, wrong argument

        throw new SpecificError('A', 'B'); //ok


        Also, would note that js allows to throw any expression, not only Error children, so you might not need to extend Error class.






        share|improve this answer













        It's also not clear for me why it doesn't work, however something like this seems to be ok:



        class CustomError<A: string, B: string> extends Error {

        constructor(name: A, message: B) {
        super(name);
        this.name = name;
        this.message = message;
        }
        }

        type A = 'A' | 'A1';
        type B = 'B' | 'B1';

        class SpecificError extends CustomError<A, B> {}

        //throw new SpecificError('a', 'c'); //error, wrong argument

        throw new SpecificError('A', 'B'); //ok


        Also, would note that js allows to throw any expression, not only Error children, so you might not need to extend Error class.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 1:47









        AlexAlex

        3,335621




        3,335621






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53367318%2fhow-to-extend-the-error-interface-with-generic-strings-in-flow%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            The term 'EXEC' is not recognized as the name of a cmdlet Powershell

            NPM command prompt closes immediately [closed]

            Error binding properties and functions in emscripten