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

查看:119
本文介绍了Typescript 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:属性 'flatMap' 在类型 'any[]' 上不存在.

顺便说一下,我正在使用 Angular 6,它使用 Typescript ~2.9.2 并且我已经包含了 import 'core-js/es7/array';polyfills.ts 中.

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.

这篇关于Typescript flatMap, flat, flatten 在类型 any[] 上不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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