TypeScript: 'esnext.array' doesn't enable access to Array.prototype.flat()
TypeScript Version: 3.1.0-dev.201xxxxx
Search Terms: esnext, esnext.array, Array.prototype.flat()
Code
// greeter.ts
function greeter(persons: string[]) {
return "Hello, " + persons.flat().join();
}
var user: string[] = ["Jane User"];
document.body.innerHTML = greeter(user);
// tsconfig.json
{
"files": [
"greeter.ts"
],
"compilerOptions": {
"target": "esnext",
"lib": [
"es2017",
"esnext.array",
"dom"
]
}
}
Expected behavior: Should compile successfully. According to MDN, it’s an experimental API supported by multiple browsers already.
Actual behavior: Compilation error:
greeter.ts:2:32 - error TS2339: Property 'flat' does not exist on type 'string[]'. 2 return "Hello, " + persons.flat().join(); ~~~~
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 16
- Comments: 26 (9 by maintainers)
@Favna I’ve tried loading the lib configs in your order as well, but that does not work for me.
@AbakumovAlexandr I thought I was facing the same issue just now (which is why I even found this open issue to begin with). I have TypeScript v3.2.2 and my
libproperty intsconfigwas set to"lib": ["es2018", "dom", "esnext.array"],being that I just added theesnext.arrayvalue. When I movedesnext.arrayto the front however ("lib": ["esnext.array", "es2018", "dom"],) the error went away and the code compiled normally.Just reporting my findings as this may be the resolution for your problem as well, in which case this issue could maybe be closed, or if the order shouldn’t matter (not too sure, never faced it before) then changed.
TypeScript transpiles language syntax, not functionality.
async/awaitis language syntax,Array.prototype.flat()is functionality. Items described in a type library (lib) are not transpiled.Even with syntax there are certain caveats, like
--downlevelIterationis behind a flag, because it requires that other functionality needs to be polyfilled in environments where it is targeted, which TypeScript won’t provide.@ernie-h I’ve got 2 more ideas then
array.prototype.flatis not available in Node 10 LTS, but it is when setting it to Node 11 Current. So if you have lower, consider upgrading. If you use Linux or MacOS you could consider using NVM so you can switch back easily if that’s not the resolution.Note that I personally use Node 11.
moduleResolutiontonodeUpdating to Node 11 also resolved this issue for me. The ordering of items in
libdoes not seem to have an effect, nor doesmoduleResolution.TypeScript has never downleveled method calls to built-in APIs
Ye Node 11 is the reason why it works as it has the feature internally (and also the fact that the newer V8 engine it has supports it). I think on that note this issue can pretty much be closed. Maybe the feature will eventually be backported to LTS (V10 at time of writing) but that is far outside the scope of this issue.
@AbakumovAlexandr Is there a solution for this? I am receiving compile errors even with
"lib": ["es6", "dom", "esnext.array"]in mytsconfigVersion 3.3.1.Compile error message:
TS2339: Property 'flat' does not exist on type 'IResult[][]'.@kitsonk I think I’ve figured out the source of the issue thanks to the feedback from your side!
I’ve switched local TypeScript versions only, while my global one stayed 2.9.2. So it’s always compliled with TS 2.9.2. But in 2.9.2,
flat()was calledflatten(). That’s why it couldn’t find it.Unfortunately,
tscdoesn’t print its version to the console leaving a room for confusion. If you, guys, feel like you’d like to implement it, let me know and I’ll create a different issue for that.