简体   繁体   English

类型 any[] 上不存在打字稿 flatMap、flat、flatten

[英]Typescript flatMap, flat, flatten doesn't exist on type any[]

I'm using chrome 70 and chrome does add methods .flatMap, .flatten, .flat .我正在使用 chrome 70 并且 chrome 确实添加了方法.flatMap, .flatten, .flat So my code does run as expected.所以我的代码确实按预期运行。 Unfortunately, Typescript doesn't like it.不幸的是,Typescript 不喜欢它。

// data.flatMap lint error
export const transformData = (data: any[]) => data.flatMap(abc => [
   parentObj(abc),
   ...generateTasks(abc)
]);

the warning i got is TS2339: Property 'flatMap' does not exist on type 'any[]'.我得到的警告是TS2339: Property 'flatMap' does not exist on type 'any[]'.

By the way I'm using Angular 6 , which use Typescript ~2.9.2 and I already include import 'core-js/es7/array';顺便说一下,我使用的是Angular 6 ,它使用Typescript ~2.9.2并且我已经包含了import 'core-js/es7/array'; in polyfills.ts .polyfills.ts

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.我的猜测是这些方法没有类型,我确实尝试npm run -dev @types/array.prototype.flatmap但仍然没有解决。

You should add es2019 or es2019.array to your --lib setting for TypeScript to recognize array.flat() and flatMap() .您应该将es2019es2019.array添加到您的--lib设置中,以便 TypeScript 识别array.flat()flatMap()

Example:例子:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "es2019"
    ]
  }
}

Previously this was available as part of esnext or esnext.array , but it's now officially part of ES2019.以前这作为esnextesnext.array一部分可用,但现在正式成为 ES2019 的一部分。

To flat single level array扁平化单层阵列

arr.reduce((acc, val) => acc.concat(val), []);

To flat multi level array扁平化多级阵列

function flatDeep(arr, d = 1) {
   return d > 0 ? arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatDeep(val, d - 1) : val), []) : arr.slice();
};

to know deeply you can also check below link要深入了解,您还可以查看以下链接

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

Aaron Beall's answer is excellent. Aaron Beall 的回答非常好。 It may be worth knowing that if "lib" is not specified in the tsConfig.JSON file a default list of libraries are injected.如果在 tsConfig.JSON 文件中未指定“lib”,则会注入默认的库列表,这可能是值得知道的。 The default libraries injected are: ► For --target ES5: DOM,ES5,ScriptHost ► For --target ES6: DOM,ES6,DOM.Iterable,ScriptHost注入的默认库是: ► 对于 --target ES5: DOM,ES5,ScriptHost ► 对于 --target ES6: DOM,ES6,DOM.Iterable,ScriptHost

In other words: We must specify those libs that were previously added automatically.换句话说:我们必须指定之前自动添加的那些库。 (see: https://www.typescriptlang.org/docs/handbook/compiler-options.html for further info) (请参阅: https : //www.typescriptlang.org/docs/handbook/compiler-options.html了解更多信息)

"compilerOptions": {

   "target": "es6",

   "lib": [ "es2019", "DOM", "ES6" ,"DOM.Iterable", "ScriptHost"],}

You can extend the global array interface while you wait for stability, at which point it will be added to the default library.您可以在等待稳定的同时扩展全局数组接口,此时它将被添加到默认库中。

interface Array<T> {
    flat(): Array<T>;
    flatMap(func: (x: T) => T): Array<T>;
}

You can also add esnext to your --lib instead of 'es2019'您还可以将esnext添加到您的 --lib 而不是 'es2019'

"compilerOptions": {
    "target": "es2015",
    "lib": ["dom", "esnext"]
}

It worked for me.它对我有用。

As of angular 11 and thx to typescript 3.9 this is now the new config.从 angular 11 和 thx 到typescript 3.9,这是现在的新配置。

"compilerOptions": {
    "target": "es2018",
    "module": "es2020",
    "lib": ["es2020", "dom"],
}

Why es2020 instead of esnext ?为什么是es2020而不是esnext

In TypeScript 3.9, the behavior of the TypeScript compiler controlled by module is the same with both "esnext" and "es2020" values.在 TypeScript 3.9 中,模块控制的 TypeScript 编译器的行为与“esnext”和“es2020”值相同。 This behavior can change in the future, because the "esnext" option could evolve in a backwards incompatible ways, resulting in build-time or run-time errors during a TypeScript update.这种行为将来可能会发生变化,因为“esnext”选项可能会以向后不兼容的方式发展,从而导致在 TypeScript 更新期间出现构建时或运行时错误。 As a result, code can become unstable.因此,代码可能会变得不稳定。 Using the "es2020" option mitigates this risk.使用“es2020”选项可以降低这种风险。

for further read 进一步阅读

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 TS2339:属性“flatMap”在类型“any”上不存在 - error TS2339: Property 'flatMap' does not exist on type 'any 打字稿抱怨 PostResponse[] 类型上不存在属性名称 - Typescript complains property name doesn't exist on type PostResponse[] Angular 2 HTTP属性重试在Observable类型上不存在<any> - Angular 2 HTTP property retryWhen doesn't exist on type Observable<any> Map 和 FlatMap 抛出错误“属性 &#39;map&#39; 在 Typescript 中的类型 &#39;unknown&#39; 上不存在 - Map and FlatMap throwing error "Property 'map' does not exist on type 'unknown' in Typescript 打字稿抱怨现有对象不存在 - Typescript complaining existing object doesn't exist 错误:“&#39;JSON&#39;类型不存在属性&#39;.get&#39;(HTML / Javascript / Angular2 / Typescript) - Error: "Property '.get' doesn't exist on type 'JSON' (HTML / Javascript / Angular2 / Typescript) FirebaseObjectObservable类型上不存在“属性&#39;...&#39; <any> 使用@Input接收的数据时发生错误” - “Property '…' doesn't exist on type FirebaseObjectObservable<any>” error when working with data received with @Input Angular4打字稿; 属性“ data”在类型“ any []”上不存在 - Angular4 Typescript; Property 'data' does not exist on type 'any[]' 打字稿错误:&#39;Observable&#39;类型中不存在属性&#39;value&#39; <any> “ - Typescript error: Property 'value' does not exist on type 'Observable<any>' Angular4和TypeScript属性在any类型上不存在[] - Angular4 and TypeScript property does not exist on type any[]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM