處理一個二維數組的時候,要修改里面某一個值,要循環查找到這項再去改值,由於比較懶不想寫循環,想到用es6的map,直接通過key獲取目標項,用set方法改值后再用[...map.values()]把map轉成我要的數組就可以了。
然鵝這么寫了后編譯報錯: error TS2568: Type 'IterableIterator<any>' is not an array type. Use compiler option '--downlevelIteration' to allow iterating of iterators.
竟然不支持還要配置啊,查找文檔了解到TypeScript 2.3 在 ES3 和 ES5 為編譯目標時由--downlevelIteration
編譯選項增加了完整的對生成器和迭代器協議的支持。
【https://www.css88.com/doc/typescript/doc/release-notes/TypeScript%202.3.html】
修改tsconfig.json就好了:
1 { 2 "compilerOptions": { 3 "target": "es5", 4 "downlevelIteration": true, 5 "lib": [ 6 "dom", 7 "es5", 8 "es2015.collection", 9 "es2015.iterable" 10 ] 11 } 12 }