spi*_*kus 5 javascript typescript
当我尝试遍历返回的值Map.values()(在Map的类型为<number,Foo>)时,Typescript给我这个错误:
错误TS2495:类型'IterableIterator <Foo>'不是数组类型或字符串类型。
根据ES6,文档 Map.values()应返回一个IterableIterableIterator,我应该能够在for-of循环中使用它。
这在node以下方面可以正常工作:
var data = [
{id: 0},
{id: 1},
{id: 3}
];
var m = new Map(data.map(n => [n.id,n]));
for(var i of m.values()) { console.log(i) }
Run Code Online (Sandbox Code Playgroud)
这给出了来自的错误tsc:
interface Foo {
id: number;
}
var data: Foo[] = [
{id: 0},
{id: 1},
{id: 2}
];
var m = new Map<number,Foo>(data.map(n => <[number,Foo]>[n.id,n]));
for(var i of m.values()) { console.log(i) }
Run Code Online (Sandbox Code Playgroud)
我从Map声明中获取,@types/core-js@0.9.34所以我想问题出在这个声明中?
其他版本和会议信息:
> tsc --version
Version 2.0.3
> cat tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true,
"typeRoots": [
"./node_modules/@types/"
]
},
"compileOnSave": true,
"exclude": [
"node_modules/*",
"**/*-aot.ts"
]
}
Run Code Online (Sandbox Code Playgroud)
我不明白一些隐含的东西tsc正在做 rel tsconfig.json。阅读tsconfig.json 文档让事情变得更清楚:
首先,我像这样进行转译,tsc mytypescript.ts这(愚蠢地)导致 Typescript 默默地忽略tsconfig.json文件,这意味着它使用默认的 ES5。但这部分有效,因为tsc仍然找到core-js包含 ES6 声明的声明,例如 Map、Iterable 等。这让我的调试有点困难。
其次,在让 Typescript 实际获取我的配置后,配置无论如何都是错误的。我实际上不需要或想要这些声明@types/core-js(非常确定)。是的,我将在我的项目中使用 core-js 作为 polyfill,但是 Typescript带有它自己的ES6 东西的声明typescript/lib/lib.es6.d.ts,并且 中的那些@types/core-js是旧的奇怪和蹩脚的东西......