Constant values as array element gives error












0














I am new to Typescript.
I have these enum and constant variables:



enum VALUE_MAP = { value1 = 'value1', value2 = 'value2', value3 = 'value3' }
const SOME_CONSTANT = [VALUE_MAP.value1, VALUE_MAP.value2];


And a method which uses SOME_CONSTANT to check if value exists.



export const hasValue = (value: string) => SOME_CONSTANT.includes(value);


This gives me error:



TS2345: Argument of type 'string' is not assignable to parameter of type 'VALUE_MAP'.



Using (value: VALUE_MAP) => will solve the issue, but I don't want to do that or may be I want to know why VALUE_MAP is getting used as type



Any help?










share|improve this question




















  • 2




    i'd use an enum for this case.
    – Daniel A. White
    Nov 20 at 14:17










  • to clarify if your object was like this const VALUE_MAP = { value1: 'valueA', value2: 'valueB', value3: 'valueC' } when calling hasValue you would pass value1 or valueA?
    – RezaRahmati
    Nov 20 at 14:19










  • It's inferring the type from the way you've declared VALUE_MAP. I'm guessing that const VALUE_MAP: any = {...} would solve this in this case, but is a workaround.... enum is def the way to go per @DanielA.White comment.
    – cale_b
    Nov 20 at 14:19












  • @DanielA.White Forgot to mention but VALUE_MAP is enum.
    – Rahul Sagore
    Nov 21 at 16:24












  • @DanielA.White Thanks for hint, Changing VALUE_MAP to const fixed the errors.
    – Rahul Sagore
    Nov 21 at 16:48


















0














I am new to Typescript.
I have these enum and constant variables:



enum VALUE_MAP = { value1 = 'value1', value2 = 'value2', value3 = 'value3' }
const SOME_CONSTANT = [VALUE_MAP.value1, VALUE_MAP.value2];


And a method which uses SOME_CONSTANT to check if value exists.



export const hasValue = (value: string) => SOME_CONSTANT.includes(value);


This gives me error:



TS2345: Argument of type 'string' is not assignable to parameter of type 'VALUE_MAP'.



Using (value: VALUE_MAP) => will solve the issue, but I don't want to do that or may be I want to know why VALUE_MAP is getting used as type



Any help?










share|improve this question




















  • 2




    i'd use an enum for this case.
    – Daniel A. White
    Nov 20 at 14:17










  • to clarify if your object was like this const VALUE_MAP = { value1: 'valueA', value2: 'valueB', value3: 'valueC' } when calling hasValue you would pass value1 or valueA?
    – RezaRahmati
    Nov 20 at 14:19










  • It's inferring the type from the way you've declared VALUE_MAP. I'm guessing that const VALUE_MAP: any = {...} would solve this in this case, but is a workaround.... enum is def the way to go per @DanielA.White comment.
    – cale_b
    Nov 20 at 14:19












  • @DanielA.White Forgot to mention but VALUE_MAP is enum.
    – Rahul Sagore
    Nov 21 at 16:24












  • @DanielA.White Thanks for hint, Changing VALUE_MAP to const fixed the errors.
    – Rahul Sagore
    Nov 21 at 16:48
















0












0








0







I am new to Typescript.
I have these enum and constant variables:



enum VALUE_MAP = { value1 = 'value1', value2 = 'value2', value3 = 'value3' }
const SOME_CONSTANT = [VALUE_MAP.value1, VALUE_MAP.value2];


And a method which uses SOME_CONSTANT to check if value exists.



export const hasValue = (value: string) => SOME_CONSTANT.includes(value);


This gives me error:



TS2345: Argument of type 'string' is not assignable to parameter of type 'VALUE_MAP'.



Using (value: VALUE_MAP) => will solve the issue, but I don't want to do that or may be I want to know why VALUE_MAP is getting used as type



Any help?










share|improve this question















I am new to Typescript.
I have these enum and constant variables:



enum VALUE_MAP = { value1 = 'value1', value2 = 'value2', value3 = 'value3' }
const SOME_CONSTANT = [VALUE_MAP.value1, VALUE_MAP.value2];


And a method which uses SOME_CONSTANT to check if value exists.



export const hasValue = (value: string) => SOME_CONSTANT.includes(value);


This gives me error:



TS2345: Argument of type 'string' is not assignable to parameter of type 'VALUE_MAP'.



Using (value: VALUE_MAP) => will solve the issue, but I don't want to do that or may be I want to know why VALUE_MAP is getting used as type



Any help?







javascript reactjs typescript constants






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 16:37

























asked Nov 20 at 14:13









Rahul Sagore

7081028




7081028








  • 2




    i'd use an enum for this case.
    – Daniel A. White
    Nov 20 at 14:17










  • to clarify if your object was like this const VALUE_MAP = { value1: 'valueA', value2: 'valueB', value3: 'valueC' } when calling hasValue you would pass value1 or valueA?
    – RezaRahmati
    Nov 20 at 14:19










  • It's inferring the type from the way you've declared VALUE_MAP. I'm guessing that const VALUE_MAP: any = {...} would solve this in this case, but is a workaround.... enum is def the way to go per @DanielA.White comment.
    – cale_b
    Nov 20 at 14:19












  • @DanielA.White Forgot to mention but VALUE_MAP is enum.
    – Rahul Sagore
    Nov 21 at 16:24












  • @DanielA.White Thanks for hint, Changing VALUE_MAP to const fixed the errors.
    – Rahul Sagore
    Nov 21 at 16:48
















  • 2




    i'd use an enum for this case.
    – Daniel A. White
    Nov 20 at 14:17










  • to clarify if your object was like this const VALUE_MAP = { value1: 'valueA', value2: 'valueB', value3: 'valueC' } when calling hasValue you would pass value1 or valueA?
    – RezaRahmati
    Nov 20 at 14:19










  • It's inferring the type from the way you've declared VALUE_MAP. I'm guessing that const VALUE_MAP: any = {...} would solve this in this case, but is a workaround.... enum is def the way to go per @DanielA.White comment.
    – cale_b
    Nov 20 at 14:19












  • @DanielA.White Forgot to mention but VALUE_MAP is enum.
    – Rahul Sagore
    Nov 21 at 16:24












  • @DanielA.White Thanks for hint, Changing VALUE_MAP to const fixed the errors.
    – Rahul Sagore
    Nov 21 at 16:48










2




2




i'd use an enum for this case.
– Daniel A. White
Nov 20 at 14:17




i'd use an enum for this case.
– Daniel A. White
Nov 20 at 14:17












to clarify if your object was like this const VALUE_MAP = { value1: 'valueA', value2: 'valueB', value3: 'valueC' } when calling hasValue you would pass value1 or valueA?
– RezaRahmati
Nov 20 at 14:19




to clarify if your object was like this const VALUE_MAP = { value1: 'valueA', value2: 'valueB', value3: 'valueC' } when calling hasValue you would pass value1 or valueA?
– RezaRahmati
Nov 20 at 14:19












It's inferring the type from the way you've declared VALUE_MAP. I'm guessing that const VALUE_MAP: any = {...} would solve this in this case, but is a workaround.... enum is def the way to go per @DanielA.White comment.
– cale_b
Nov 20 at 14:19






It's inferring the type from the way you've declared VALUE_MAP. I'm guessing that const VALUE_MAP: any = {...} would solve this in this case, but is a workaround.... enum is def the way to go per @DanielA.White comment.
– cale_b
Nov 20 at 14:19














@DanielA.White Forgot to mention but VALUE_MAP is enum.
– Rahul Sagore
Nov 21 at 16:24






@DanielA.White Forgot to mention but VALUE_MAP is enum.
– Rahul Sagore
Nov 21 at 16:24














@DanielA.White Thanks for hint, Changing VALUE_MAP to const fixed the errors.
– Rahul Sagore
Nov 21 at 16:48






@DanielA.White Thanks for hint, Changing VALUE_MAP to const fixed the errors.
– Rahul Sagore
Nov 21 at 16:48














3 Answers
3






active

oldest

votes


















1














You should use:



export const hasValue = (value) => SOME_CONSTANT.includes(value);


Without string for param.
If you use Babel plugins, this will strip parameter types during transpiling.



Input:



function foo(one: string, two: number): string {}


Output:



function foo(one, two) {}


So, parameter types are not valid in ES6. You can use them if the code is transpiled using Babel(with the stripping plugins).






share|improve this answer





















  • The question is tagged with TypeScript, so I would guess the TypeScript compiler is being used.
    – Fenton
    Nov 20 at 14:33










  • Removing string in one: string works. But if use SOME_CONSTANT somewhere else, like in render method, it gives same error . Like SOME_CONSTANT.includes(this.props.value) gives error.
    – Rahul Sagore
    Nov 21 at 16:35



















1














You need to target ES2016 or newer if you want to use Array.includes as it didn't exist in ECMAScript 5.



For example, this tsconfig.json fails with the error you describe:



{
"compilerOptions": {
"target": "ES5"
}
}


And this tsconfig.json doesn't:



{
"compilerOptions": {
"target": "es2016"
}
}





share|improve this answer





















  • Tried that. Still got the same error
    – Rahul Sagore
    Nov 21 at 16:27










  • I have a working version in VSCode with this config. What tools are you using, and how are you compiling?
    – Fenton
    Nov 22 at 8:38



















0














After @Daniel A. White, pointed that I should be using enum. Then I got a little hint and changed a little bit of code.
So VALUE_MAP was already an enum. I changed it to constant, and it worked. Thanks everyone for you time and suggestion.



Not getting errors now.






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%2f53394929%2fconstant-values-as-array-element-gives-error%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You should use:



    export const hasValue = (value) => SOME_CONSTANT.includes(value);


    Without string for param.
    If you use Babel plugins, this will strip parameter types during transpiling.



    Input:



    function foo(one: string, two: number): string {}


    Output:



    function foo(one, two) {}


    So, parameter types are not valid in ES6. You can use them if the code is transpiled using Babel(with the stripping plugins).






    share|improve this answer





















    • The question is tagged with TypeScript, so I would guess the TypeScript compiler is being used.
      – Fenton
      Nov 20 at 14:33










    • Removing string in one: string works. But if use SOME_CONSTANT somewhere else, like in render method, it gives same error . Like SOME_CONSTANT.includes(this.props.value) gives error.
      – Rahul Sagore
      Nov 21 at 16:35
















    1














    You should use:



    export const hasValue = (value) => SOME_CONSTANT.includes(value);


    Without string for param.
    If you use Babel plugins, this will strip parameter types during transpiling.



    Input:



    function foo(one: string, two: number): string {}


    Output:



    function foo(one, two) {}


    So, parameter types are not valid in ES6. You can use them if the code is transpiled using Babel(with the stripping plugins).






    share|improve this answer





















    • The question is tagged with TypeScript, so I would guess the TypeScript compiler is being used.
      – Fenton
      Nov 20 at 14:33










    • Removing string in one: string works. But if use SOME_CONSTANT somewhere else, like in render method, it gives same error . Like SOME_CONSTANT.includes(this.props.value) gives error.
      – Rahul Sagore
      Nov 21 at 16:35














    1












    1








    1






    You should use:



    export const hasValue = (value) => SOME_CONSTANT.includes(value);


    Without string for param.
    If you use Babel plugins, this will strip parameter types during transpiling.



    Input:



    function foo(one: string, two: number): string {}


    Output:



    function foo(one, two) {}


    So, parameter types are not valid in ES6. You can use them if the code is transpiled using Babel(with the stripping plugins).






    share|improve this answer












    You should use:



    export const hasValue = (value) => SOME_CONSTANT.includes(value);


    Without string for param.
    If you use Babel plugins, this will strip parameter types during transpiling.



    Input:



    function foo(one: string, two: number): string {}


    Output:



    function foo(one, two) {}


    So, parameter types are not valid in ES6. You can use them if the code is transpiled using Babel(with the stripping plugins).







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 at 14:22









    oma

    83447




    83447












    • The question is tagged with TypeScript, so I would guess the TypeScript compiler is being used.
      – Fenton
      Nov 20 at 14:33










    • Removing string in one: string works. But if use SOME_CONSTANT somewhere else, like in render method, it gives same error . Like SOME_CONSTANT.includes(this.props.value) gives error.
      – Rahul Sagore
      Nov 21 at 16:35


















    • The question is tagged with TypeScript, so I would guess the TypeScript compiler is being used.
      – Fenton
      Nov 20 at 14:33










    • Removing string in one: string works. But if use SOME_CONSTANT somewhere else, like in render method, it gives same error . Like SOME_CONSTANT.includes(this.props.value) gives error.
      – Rahul Sagore
      Nov 21 at 16:35
















    The question is tagged with TypeScript, so I would guess the TypeScript compiler is being used.
    – Fenton
    Nov 20 at 14:33




    The question is tagged with TypeScript, so I would guess the TypeScript compiler is being used.
    – Fenton
    Nov 20 at 14:33












    Removing string in one: string works. But if use SOME_CONSTANT somewhere else, like in render method, it gives same error . Like SOME_CONSTANT.includes(this.props.value) gives error.
    – Rahul Sagore
    Nov 21 at 16:35




    Removing string in one: string works. But if use SOME_CONSTANT somewhere else, like in render method, it gives same error . Like SOME_CONSTANT.includes(this.props.value) gives error.
    – Rahul Sagore
    Nov 21 at 16:35













    1














    You need to target ES2016 or newer if you want to use Array.includes as it didn't exist in ECMAScript 5.



    For example, this tsconfig.json fails with the error you describe:



    {
    "compilerOptions": {
    "target": "ES5"
    }
    }


    And this tsconfig.json doesn't:



    {
    "compilerOptions": {
    "target": "es2016"
    }
    }





    share|improve this answer





















    • Tried that. Still got the same error
      – Rahul Sagore
      Nov 21 at 16:27










    • I have a working version in VSCode with this config. What tools are you using, and how are you compiling?
      – Fenton
      Nov 22 at 8:38
















    1














    You need to target ES2016 or newer if you want to use Array.includes as it didn't exist in ECMAScript 5.



    For example, this tsconfig.json fails with the error you describe:



    {
    "compilerOptions": {
    "target": "ES5"
    }
    }


    And this tsconfig.json doesn't:



    {
    "compilerOptions": {
    "target": "es2016"
    }
    }





    share|improve this answer





















    • Tried that. Still got the same error
      – Rahul Sagore
      Nov 21 at 16:27










    • I have a working version in VSCode with this config. What tools are you using, and how are you compiling?
      – Fenton
      Nov 22 at 8:38














    1












    1








    1






    You need to target ES2016 or newer if you want to use Array.includes as it didn't exist in ECMAScript 5.



    For example, this tsconfig.json fails with the error you describe:



    {
    "compilerOptions": {
    "target": "ES5"
    }
    }


    And this tsconfig.json doesn't:



    {
    "compilerOptions": {
    "target": "es2016"
    }
    }





    share|improve this answer












    You need to target ES2016 or newer if you want to use Array.includes as it didn't exist in ECMAScript 5.



    For example, this tsconfig.json fails with the error you describe:



    {
    "compilerOptions": {
    "target": "ES5"
    }
    }


    And this tsconfig.json doesn't:



    {
    "compilerOptions": {
    "target": "es2016"
    }
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 at 14:39









    Fenton

    151k42285309




    151k42285309












    • Tried that. Still got the same error
      – Rahul Sagore
      Nov 21 at 16:27










    • I have a working version in VSCode with this config. What tools are you using, and how are you compiling?
      – Fenton
      Nov 22 at 8:38


















    • Tried that. Still got the same error
      – Rahul Sagore
      Nov 21 at 16:27










    • I have a working version in VSCode with this config. What tools are you using, and how are you compiling?
      – Fenton
      Nov 22 at 8:38
















    Tried that. Still got the same error
    – Rahul Sagore
    Nov 21 at 16:27




    Tried that. Still got the same error
    – Rahul Sagore
    Nov 21 at 16:27












    I have a working version in VSCode with this config. What tools are you using, and how are you compiling?
    – Fenton
    Nov 22 at 8:38




    I have a working version in VSCode with this config. What tools are you using, and how are you compiling?
    – Fenton
    Nov 22 at 8:38











    0














    After @Daniel A. White, pointed that I should be using enum. Then I got a little hint and changed a little bit of code.
    So VALUE_MAP was already an enum. I changed it to constant, and it worked. Thanks everyone for you time and suggestion.



    Not getting errors now.






    share|improve this answer


























      0














      After @Daniel A. White, pointed that I should be using enum. Then I got a little hint and changed a little bit of code.
      So VALUE_MAP was already an enum. I changed it to constant, and it worked. Thanks everyone for you time and suggestion.



      Not getting errors now.






      share|improve this answer
























        0












        0








        0






        After @Daniel A. White, pointed that I should be using enum. Then I got a little hint and changed a little bit of code.
        So VALUE_MAP was already an enum. I changed it to constant, and it worked. Thanks everyone for you time and suggestion.



        Not getting errors now.






        share|improve this answer












        After @Daniel A. White, pointed that I should be using enum. Then I got a little hint and changed a little bit of code.
        So VALUE_MAP was already an enum. I changed it to constant, and it worked. Thanks everyone for you time and suggestion.



        Not getting errors now.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 10:52









        Rahul Sagore

        7081028




        7081028






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53394929%2fconstant-values-as-array-element-gives-error%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

            Ottavio Pratesi

            Tricia Helfer

            15 giugno