当前位置:网站首页>Use compiler option '--downleveliteration' to allow iteration of iterations

Use compiler option '--downleveliteration' to allow iteration of iterations

2022-06-11 11:41:00 Michael18811380328

Use compiler option '--downlevelIteration' to allow iterating of iterators Error reporting solution

Recently used in a project TS Unit test , The interface reports an error as follows :

 

Error reporting analysis

The specific error information is as follows :Set<number> Not array or string type , Cannot use extended operator .

error TS2569: Type 'Set<number>' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.

let newArr:number[] = [...new Set(nums)].sort((a, b) => a - b);

After Baidu , Other netizens reported similar mistakes ,Map perhaps Set You cannot use the extension operator directly

TS2569: Type 'Map' is not an array type or a string type. Use compiler. option '- downlevellteration' to allow iterating of iterators.

 

Solution 1

Follow official tips , Set up '--downlevelIteration' yes true, Allow to use “ ES5” or “ ES3” When it's a goal , stay “ for-of”, spread 、 Support for iteratible items in Deconstruction .

hold tsconfig.json Just modify the configuration file

{
  "compilerOptions": {
    /* Basic Options */
     "downlevelIteration": true,
/* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
}

Solution 2

To configure dom.iterable and downlevelIteration It can run normally

tsconfig.json

{
    /* When the goal is ES5 or ES3 Provide information about for-of、 Complete support for iterators in extension operators and deconstruction assignment */
    "downlevelIteration": true,
    "lib": [
      "dom",
      "es5",
      "es6",
      "es7",
      "dom.iterable"
    ]
}
  • Set up target=es6 When , It can be executed normally . reason :

Be careful : If not specified --lib, The default library list will be injected . The default library for injection is : ► For --target ES5: DOM,ES5,ScriptHost ► For --target ES6: DOM,ES6,DOM.Iterable,ScriptHost

Reference link

https://stackoverflow.com/questions/62437679/typescript-use-compiler-option-downleveliteration-to-allow-iterating-of-iter

http://www.voidcn.com/article/p-bccbaiiq-bym.html

https://cloud.tencent.com/developer/article/1593335

原网站

版权声明
本文为[Michael18811380328]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111128511305.html