【发布时间】:2021-09-26 07:53:37
【问题描述】:
属性 'flat' 在类型 'string[][]' 上不存在
SO 上存在几个类似的问题。他们的回答是将es2019 添加到tsconfig.json 中的--lib 标记中,但是这样做对我的问题没有帮助。
$ node --version
v14.17.1
$ tsc --version
Version 4.3.5
$ tsc src/index.ts
src/index.ts:6:41 - error TS2550: Property 'flat' does not exist on type 'string[][]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2019' or later.
6 let flat_array: string[] = nested_array.flat();
src/index.ts
let nested_array: string[][] = [
["1", "2"],
["3", "4"],
];
let flat_array: string[] = nested_array.flat();
console.log(flat_array);
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"es2019",
"DOM"
],
"module": "commonjs",
"declaration": true,
"outDir": "./lib",
"strict": true
},
"include": ["src/"],
"exclude": ["node_modules", "**/__tests__/*"]
}
我尝试过旧版本的 node (v10),以及不同版本的 typescript (3.7.3)。我已经将开发依赖项中的 typescript 作为 npm 脚本运行,并且还使用了全局安装的 typescript。这个错误不会消失。我也尝试关闭并重新打开 VSCode。
如果您有兴趣,这里也是package.json 文件。除了全局安装的打字稿外,我还尝试使用npm start 运行,但这会引发相同的错误。
package.json
{
"name": "test-typescript",
"version": "1.0.0",
"description": "",
"main": "src/index.ts",
"scripts": {
"start": "tsc src/index.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "16.3.3",
"typescript": "4.3.5"
},
"dependencies": {}
}
【问题讨论】:
-
"target": "es5"- 我认为你需要改变这个,而不仅仅是 lib
标签: typescript