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(); ~~~~

Playground Link

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 16
  • Comments: 26 (9 by maintainers)

Most upvoted comments

@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 lib property in tsconfig was set to "lib": ["es2018", "dom", "esnext.array"], being that I just added the esnext.array value. When I moved esnext.array to 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/await is 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 --downlevelIteration is 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

  1. What is your NodeJS version (node -v)? I know for a fact that on runkit.com array.prototype.flat is 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.

  1. Here is an exact copy of my tsconfig, maybe you need to configure other settings, such as the moduleResolution to node
{
	"compilerOptions": {
		"outDir": "dist/",
		"rootDir": "./src",
		"forceConsistentCasingInFileNames": true,
		"esModuleInterop": true,
		"lib": ["esnext.array", "es2018", "dom"],
		"target": "es6",
		"module": "commonjs",
		"moduleResolution": "node",
		"newLine": "lf",
		"noFallthroughCasesInSwitch": true,
		"noImplicitAny": true,
		"noImplicitReturns": true,
		"noImplicitThis": true,
		"noUnusedLocals": true,
		"removeComments": true,
		"resolveJsonModule": true
	},
	"include": ["src/**/*.ts"],
	"exclude": ["node_modules", "dist", "src/**/*.js"]
}

Updating to Node 11 also resolved this issue for me. The ordering of items in lib does not seem to have an effect, nor does moduleResolution.

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 my tsconfig Version 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 called flatten(). That’s why it couldn’t find it.

Unfortunately, tsc doesn’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.