素のディレクトリでまだハマっている所がある。サンプルコードは
var map:Map<number, string>; map = new Map<number, string>(); map.set(3, "hello"); console.log(map.get(3)); map.set(5, "world"); console.log(map.get(5)); for (let [key, val] of map) { console.log(`${key} = ${val}`) }
tscの設定:
TsDevEtc > tsc --showConfig { "compilerOptions": { "target": "es2021", "lib": [ "es2021", "dom", "es2021.promise", "es2021.weakref" ], "module": "commonjs", "downlevelIteration": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, "skipLibCheck": true }, "files": [ "./expo_k.ts", "./map.ts", "./mapIter.ts", "./polynom.ts" ] }
"downlevelIteration": true に注目。設定されている。それで:
TsDevEtc > tsc .\mapIter.ts mapIter.ts:12:24 - error TS2569: Type 'Map<number, string>' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators. 12 for (let [key, val] of map) { ~~~ Found 1 error in mapIter.ts:12
Use compiler option '--downlevelIteration' to allow iterating of iterators. と言っているが、そのオプションは設定しているって。で、コマンドラインから設定する。
TsDevEtc > tsc --downlevelIteration .\mapIter.ts
コンパイル成功。nodeでの実行も成功。
今回の問題点は: tsconfig.json に設定しており、tsc --showConfig でも表示されるオプションが何故コンパイル時には効かないのか?
わかった!
tsc は次のどちらかのモードで動作する。
- すべての情報を tsconfig.json から取得してコンパイルする。
- すべての情報をコマンドラインから取得してコンパイルする。
コマンドライン引数を書くと「すべての情報をコマンドラインから取得〉モードになるので tsconfig.json は無視される。
僕は次だと思っていた。
- コマンドライン引数があれば、その情報を優先して、残りの情報は設定ファイルや環境変数から取る。
たぶん、上のような常識は今や常識ではなくて通用しないのだろう。コマンドラインから何かするってのがメジャーじゃないから、コマンドライン使いは迫害されるのだろう。