I'm trying to make my Angular 2 app compile when using JavaScript array experimental features like flat()
.
I've added esnext.array
option to tsconfig.json
, so the lib
section looks like this:
"lib": [
"es2017",
"esnext.array",
"dom"
],
I've checked lib.esnext.array.d.ts
file and it indeed contains definitions for the flat()
method.
But despite this, I'm getting a compilation error:
ERROR in src/app/services/service.ts(286,22): error TS2339: Property 'flat' does not exist on type 'ReadonlyMap[]'.
UPDATE 1: I've checked esnext
option which results in the same error.
Also, I've checked that Angular CLI picks up the lib
options by typing something non-existent like esnext.array.dfgdfg
.
UPDATE 2: To exclude a possibility that it's something wrong with the TypeScript version 2.9.2 or Angular toolchain, I've created a simple project with pure TypeScript:
// greeter.ts
function greeter(persons: string[]) {
return "Hello, " + persons.flat().join();
}
var user: string[] = ["Jane User"];
document.body.innerHTML = greeter(user);
// tsconfig.json
{
"compilerOptions": {
"target": "esnext",
"lib": [
"es2017",
"esnext.array",
"dom"
]
}
}
And tried to compile it with TypsScript 2.9.2, the current latest 3.0.1 and typescript@next
, but I always get the same error:
greeter.ts:2:32 - error TS2339: Property 'flat' does not exist on type 'string[]'.
2 return "Hello, " + persons.flat().join(); ~~~~
Could somebody explain what I'm missing here?
There's probably a Node.js v10/v11 compatibility issue here.
A temporary working solution is to use Lodash's flatten
:
Instead of array.flat()
use _.flatten(array)
In older TypeScript versions, flat()
has been called flatten()
. So if you're stuck with those TS versions, you might try using flatten()
instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.