TypeScript Compiler - Why downlevelIteration is not on by default?












4















When targeting like ES5 with usage of spread operator ... to convert an Iterator to an Array, it shows the error to use -downlevelIteration compiler option. Once it is on, spread operators seem just work flawlessly. I wonder why is there a need to specify this?



Are there any downsides/limitation when it is enabled besides adding more emitted generated code from tslib?



Without -downlevelIteration enabled by default in the TypeScript Playground spread operators can't be fully tested.



Another example: Dynamically create array of N (eg. 3):



[...Array(3).keys()]  // output: [0, 1, 2]


Error display in VS Code:
enter image description here



Error message from tsc:




Type 'IterableIterator' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.




Edit and view the code and error in TypeScript Playground










share|improve this question

























  • The generated code is pretty ugly as well

    – Titian Cernicova-Dragomir
    Nov 23 '18 at 6:25
















4















When targeting like ES5 with usage of spread operator ... to convert an Iterator to an Array, it shows the error to use -downlevelIteration compiler option. Once it is on, spread operators seem just work flawlessly. I wonder why is there a need to specify this?



Are there any downsides/limitation when it is enabled besides adding more emitted generated code from tslib?



Without -downlevelIteration enabled by default in the TypeScript Playground spread operators can't be fully tested.



Another example: Dynamically create array of N (eg. 3):



[...Array(3).keys()]  // output: [0, 1, 2]


Error display in VS Code:
enter image description here



Error message from tsc:




Type 'IterableIterator' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.




Edit and view the code and error in TypeScript Playground










share|improve this question

























  • The generated code is pretty ugly as well

    – Titian Cernicova-Dragomir
    Nov 23 '18 at 6:25














4












4








4








When targeting like ES5 with usage of spread operator ... to convert an Iterator to an Array, it shows the error to use -downlevelIteration compiler option. Once it is on, spread operators seem just work flawlessly. I wonder why is there a need to specify this?



Are there any downsides/limitation when it is enabled besides adding more emitted generated code from tslib?



Without -downlevelIteration enabled by default in the TypeScript Playground spread operators can't be fully tested.



Another example: Dynamically create array of N (eg. 3):



[...Array(3).keys()]  // output: [0, 1, 2]


Error display in VS Code:
enter image description here



Error message from tsc:




Type 'IterableIterator' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.




Edit and view the code and error in TypeScript Playground










share|improve this question
















When targeting like ES5 with usage of spread operator ... to convert an Iterator to an Array, it shows the error to use -downlevelIteration compiler option. Once it is on, spread operators seem just work flawlessly. I wonder why is there a need to specify this?



Are there any downsides/limitation when it is enabled besides adding more emitted generated code from tslib?



Without -downlevelIteration enabled by default in the TypeScript Playground spread operators can't be fully tested.



Another example: Dynamically create array of N (eg. 3):



[...Array(3).keys()]  // output: [0, 1, 2]


Error display in VS Code:
enter image description here



Error message from tsc:




Type 'IterableIterator' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.




Edit and view the code and error in TypeScript Playground







typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 11 at 18:48









Beau Smith

21.5k64962




21.5k64962










asked Nov 23 '18 at 5:54









CerlancismCerlancism

3816




3816













  • The generated code is pretty ugly as well

    – Titian Cernicova-Dragomir
    Nov 23 '18 at 6:25



















  • The generated code is pretty ugly as well

    – Titian Cernicova-Dragomir
    Nov 23 '18 at 6:25

















The generated code is pretty ugly as well

– Titian Cernicova-Dragomir
Nov 23 '18 at 6:25





The generated code is pretty ugly as well

– Titian Cernicova-Dragomir
Nov 23 '18 at 6:25












1 Answer
1






active

oldest

votes


















1














After reading the release notes and the article TypeScript 2.3: Downlevel Iteration for ES3/ES5, I believe the answer to this question is that downlevelIteration is disabled because you need to decide (via configuration) how you want TypeScript to handle the compilation of compatibility code (to support older versions of Javascript).



As the more lengthy explanation in the article makes clear, you have to make a decision as to if you want TypeScript to inline necessary helper functions (simple, but can result in larger production bundle size) or if you wish to configure TypeScript to use tslib as a dependency and then make calls to its external methods.



I highly recommend reading TypeScript 2.3: Downlevel Iteration for ES3/ES5 for a deeper understanding… and possibly an alternate solution to your initial issue.





As for why downlevelIteration is not an option in the TypeScript Playground, I'm not sure. The Playground is open source, so you can definitely create a submit a pull request… and doing so may get an even better answer than this one!






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%2f53441292%2ftypescript-compiler-why-downleveliteration-is-not-on-by-default%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














    After reading the release notes and the article TypeScript 2.3: Downlevel Iteration for ES3/ES5, I believe the answer to this question is that downlevelIteration is disabled because you need to decide (via configuration) how you want TypeScript to handle the compilation of compatibility code (to support older versions of Javascript).



    As the more lengthy explanation in the article makes clear, you have to make a decision as to if you want TypeScript to inline necessary helper functions (simple, but can result in larger production bundle size) or if you wish to configure TypeScript to use tslib as a dependency and then make calls to its external methods.



    I highly recommend reading TypeScript 2.3: Downlevel Iteration for ES3/ES5 for a deeper understanding… and possibly an alternate solution to your initial issue.





    As for why downlevelIteration is not an option in the TypeScript Playground, I'm not sure. The Playground is open source, so you can definitely create a submit a pull request… and doing so may get an even better answer than this one!






    share|improve this answer






























      1














      After reading the release notes and the article TypeScript 2.3: Downlevel Iteration for ES3/ES5, I believe the answer to this question is that downlevelIteration is disabled because you need to decide (via configuration) how you want TypeScript to handle the compilation of compatibility code (to support older versions of Javascript).



      As the more lengthy explanation in the article makes clear, you have to make a decision as to if you want TypeScript to inline necessary helper functions (simple, but can result in larger production bundle size) or if you wish to configure TypeScript to use tslib as a dependency and then make calls to its external methods.



      I highly recommend reading TypeScript 2.3: Downlevel Iteration for ES3/ES5 for a deeper understanding… and possibly an alternate solution to your initial issue.





      As for why downlevelIteration is not an option in the TypeScript Playground, I'm not sure. The Playground is open source, so you can definitely create a submit a pull request… and doing so may get an even better answer than this one!






      share|improve this answer




























        1












        1








        1







        After reading the release notes and the article TypeScript 2.3: Downlevel Iteration for ES3/ES5, I believe the answer to this question is that downlevelIteration is disabled because you need to decide (via configuration) how you want TypeScript to handle the compilation of compatibility code (to support older versions of Javascript).



        As the more lengthy explanation in the article makes clear, you have to make a decision as to if you want TypeScript to inline necessary helper functions (simple, but can result in larger production bundle size) or if you wish to configure TypeScript to use tslib as a dependency and then make calls to its external methods.



        I highly recommend reading TypeScript 2.3: Downlevel Iteration for ES3/ES5 for a deeper understanding… and possibly an alternate solution to your initial issue.





        As for why downlevelIteration is not an option in the TypeScript Playground, I'm not sure. The Playground is open source, so you can definitely create a submit a pull request… and doing so may get an even better answer than this one!






        share|improve this answer















        After reading the release notes and the article TypeScript 2.3: Downlevel Iteration for ES3/ES5, I believe the answer to this question is that downlevelIteration is disabled because you need to decide (via configuration) how you want TypeScript to handle the compilation of compatibility code (to support older versions of Javascript).



        As the more lengthy explanation in the article makes clear, you have to make a decision as to if you want TypeScript to inline necessary helper functions (simple, but can result in larger production bundle size) or if you wish to configure TypeScript to use tslib as a dependency and then make calls to its external methods.



        I highly recommend reading TypeScript 2.3: Downlevel Iteration for ES3/ES5 for a deeper understanding… and possibly an alternate solution to your initial issue.





        As for why downlevelIteration is not an option in the TypeScript Playground, I'm not sure. The Playground is open source, so you can definitely create a submit a pull request… and doing so may get an even better answer than this one!







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 11 at 21:02

























        answered Mar 11 at 18:37









        Beau SmithBeau Smith

        21.5k64962




        21.5k64962
































            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%2f53441292%2ftypescript-compiler-why-downleveliteration-is-not-on-by-default%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

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?