打字稿flatMap,flat,flatten在any类型上不存在[] [英] Typescript flatMap, flat, flatten doesn't exist on type any[]

查看:106
本文介绍了打字稿flatMap,flat,flatten在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.

推荐答案

您应将es2019es2019.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"
    ]
  }
}

以前,它是esnextesnext.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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆