【发布时间】:2021-11-06 04:01:20
【问题描述】:
我尝试了一些 bable 设置,tsconfig 中不同的目标/lib 设置,什么都没有。不知道我还能做什么......
错误如下所示:
let test:object[] = [{}];
test.flatMap();
Property 'flatMap' does not exist on type 'object[]'. Do you need to change your target library? Try changing the `lib` compiler option to 'es2019' or later.ts(2550)
&我的 tsconfig
{
"compilerOptions": {
"target": "es5",
"lib": [
"es2020"
],
"esModuleInterop": true,
"jsx": "react",
"module": "commonjs",
"moduleResolution": "node",
"noEmit": true,
"noUnusedLocals": true,
"outDir": "public",
"resolveJsonModule": true,
"rootDir": "src",
"sourceMap": false,
"strict": true,
"strictNullChecks": true,
"allowJs": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
},
"include": ["src/**/*"],
"exclude": ["node_modules", "public"]
}
【问题讨论】:
-
您是否尝试将
target更改为es2020? -
是的,完全相同的错误。就好像我在 tsconfig 中更改的任何内容一样,没有文件识别文件中的更改。
-
更改内容后,您是在构建后还是在 IDE 上收到错误消息?您是否尝试过在 IDE 中重新启动 TS 服务器?有时配置更改不会轻易反映
-
@maazadeeb derp。重新启动 ts 服务器修复了它。如果您添加答案,我可以确认这是正确的。我在 tsconfig 中进行了其他更改,当我重建它们时它们起作用了,似乎目标/lib 更改需要重新启动服务器。
-
可能的解决方法:如果您将
lodash(以及可选的@types/lodash)添加到您的package.json,它会为您的数组添加flatMap以及大量其他方便的函数。 (例如,uniq过滤数组中的重复值,groupBy使用键属性对数组值进行分组。)
标签: typescript