我正在使用类似脚本创建一个类,该类具有ES6(ECMAscript 2016)Map的属性,如下所示:
class Item {
configs: ????;
constructor () {
this.configs = new Map();
}
}
Run Code Online (Sandbox Code Playgroud)
如何在打字稿中声明ES6 Map类型?
我在Visual Studio Code IDE中的node_modules/@types/core-js/index.d.ts中有这些错误:
当我运行npm start应用程序时,我得到:
node_modules/@types/core-js/index.d.ts(21,14): error TS2300: Duplicate identifier 'PropertyKey'.
node_modules/@types/core-js/index.d.ts(85,5): error TS2687: All declarations of 'name' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(145,5): error TS2403: Subsequent variable declarations must have the same type. Variable '[Symbol.unscopables]' must be of type '{ copyWithin: boolean; entries: boolean; fill: boolean; find: boolean; findIndex: boolean; keys: ...', but here has type 'any'.
node_modules/@types/core-js/index.d.ts(262,5): error TS2687: All declarations of 'flags' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(276,5): error TS2687: All declarations of 'EPSILON' must have …Run Code Online (Sandbox Code Playgroud) 我有带ES6目标的TypeScript项目,它用于core-js填充ES2017功能,并相应地配置tsconfig.json.
当Object.entries(...)和Object.values(...)使用时,结果不具有阵列的方法和属性(map,forEach,length等),它们显示为纯对象IDE,所以any[]类型应明确浇铸:
虽然Object.keys(...)表现得像它应该.
同时,IDE以某种方式'知道'正确的类型,Object.entries并Object.values按照lib.es2017.object.d.tsCtrl + Shift + P上的TypeScript显示它们.但它似乎忽略了检查的类型,因为覆盖ObjectConstructor当前文件解决了问题:
interface ObjectConstructor {
values(o: any): any[];
entries(o: any): [string, any][];
}
Run Code Online (Sandbox Code Playgroud)
tsc 对于打字来说似乎很好,所以它看起来像IDE特定的问题.
只有Use TypeScript service在Languages & Frameworks > TypeScript未选中时才会发生这种情况.启用TypeScript服务时,所有内容都正常(由于之前存在TS服务问题,故意禁用它).
这是tsconfig.json:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"alwaysStrict": true,
"strictNullChecks": false,
"baseUrl": "./src",
"paths": [],
"lib": [
"es2016", …Run Code Online (Sandbox Code Playgroud)