[英]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()
.您应该将
es2019
或es2019.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.以前这作为
esnext
或esnext.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"],
}
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”选项可以降低这种风险。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.