打字稿flatMap,flat,flatten在any类型上不存在[] [英] Typescript flatMap, flat, flatten doesn't exist on type any[]
问题描述
我正在使用chrome 70,chrome确实添加了方法.flatMap, .flatten, .flat
.所以我的代码确实按预期运行.不幸的是,Typescript不喜欢它.
I'm using chrome 70 and chrome does add methods .flatMap, .flatten, .flat
. So my code does run as expected. Unfortunately, Typescript doesn't like it.
// data.flatMap lint error
export const transformData = (data: any[]) => data.flatMap(abc => [
parentObj(abc),
...generateTasks(abc)
]);
我得到的警告是TS2339: Property 'flatMap' does not exist on type 'any[]'.
通过我使用Angular 6
的方式,其中使用了Typescript ~2.9.2
,并且我已经在polyfills.ts
中包含了import 'core-js/es7/array';
.
By the way I'm using Angular 6
, which use Typescript ~2.9.2
and I already include import 'core-js/es7/array';
in polyfills.ts
.
我的猜测是这些方法没有类型,我确实尝试了npm run -dev @types/array.prototype.flatmap
但仍然无法解决.
My guess is that there is no typing for these methods, and I did try to npm run -dev @types/array.prototype.flatmap
but still not solve.
推荐答案
您应将es2019
或es2019.array
添加到--lib
设置中,以便TypeScript识别array.flat()
和flatMap()
.
You should add es2019
or es2019.array
to your --lib
setting for TypeScript to recognize array.flat()
and flatMap()
.
示例:
{
"compilerOptions": {
"target": "es5",
"lib": [
"es2019"
]
}
}
以前,它是esnext
或esnext.array
的一部分,但现在正式成为ES2019的一部分.
Previously this was available as part of esnext
or esnext.array
, but it's now officially part of ES2019.
这篇关于打字稿flatMap,flat,flatten在any类型上不存在[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!