4

Property flat does not exist on type string[][]

Several similar questions exist on SO. Their answers are to add es2019 to the --lib tag in the tsconfig.json, but doing this doesn't help my issue.

$ node --version
v14.17.1
$ tsc --version
Version 4.3.5
$ tsc src/index.ts
src/index.ts:6:41 - error TS2550: Property 'flat' does not exist on type 'string[][]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2019' or later.

6 let flat_array: string[] = nested_array.flat();

src/index.ts

let nested_array: string[][] = [
  ["1", "2"],
  ["3", "4"],
];

let flat_array: string[] = nested_array.flat();

console.log(flat_array);

tsconfig.json

{
    "compilerOptions": {
      "target": "es5",
      "lib": [
        "es2019",
        "DOM"
      ],
      "module": "commonjs",
      "declaration": true,
      "outDir": "./lib",
      "strict": true
    },
    "include": ["src/"],
    "exclude": ["node_modules", "**/__tests__/*"]
  }

I've tried older version of node (v10), and different versions of typescript (3.7.3) too. I've run typescript from dev dependencies as npm script, and using globally installed typescript as well. This error does not go away. I also tried closing and re-opening VSCode.

In case you are interested, here is the package.json file too. I tried running with npm start in addition to globally installed typescript, but this throws the same error.

package.json

{
  "name": "test-typescript",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.ts",
  "scripts": {
    "start": "tsc src/index.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/node": "16.3.3",
    "typescript": "4.3.5"
  },
  "dependencies": {}
}

Alireza Ahmadi
  • 8,579
  • 5
  • 15
  • 42
NicoWheat
  • 2,157
  • 2
  • 26
  • 34

3 Answers3

2

Change target to Es2019. Here is working sample:

{
  "compilerOptions": {
    ...
    "target": "ES2019",
    "module": "ESNext",
    "moduleResolution": "node"
  }
}

Working Code:

PlaygroundLink

And the result:

enter image description here

Alireza Ahmadi
  • 8,579
  • 5
  • 15
  • 42
  • Yes, I see that it works in the playground. It is still not working on my computer. There is no 'lib' tag in your examples. I am trying with and without, etc. Even adopting your new configs does not help. How can I break the tsconfig.json file to make sure that new versions are actually being read? It is almost like the program isn't reading the newly saved tsconfig file... – NicoWheat Jul 18 '21 at 08:27
  • If I save an empty tsconfig.json file `{}` then I still get the same error. Shouldn't I get a different error regarding the lack of information in the tsconfig.json file?? – NicoWheat Jul 18 '21 at 08:30
  • I run this code in my visual studio with this config and everything it's ok. `{ "compilerOptions": { "noImplicitAny": false, "noEmitOnError": true, "removeComments": false, "sourceMap": true, "target": "ES2019" }, "exclude": [ "node_modules", "wwwroot" ]` – Alireza Ahmadi Jul 18 '21 at 08:34
  • Which version of typescript you are using? – Alireza Ahmadi Jul 18 '21 at 08:40
  • $ tsc --version Version 4.3.5 – NicoWheat Jul 18 '21 at 08:42
  • Have you tried my config in your `tsconfig.json`? Please save and restart your idea after copy and paste the config – Alireza Ahmadi Jul 18 '21 at 08:44
  • running `$ tsc index.ts` returns the error. But, running `$ tsc index.ts --target es2019` in the terminal with no tsconfig.json file does NOT throw an error, but nor does it log the resulting array like it should. – NicoWheat Jul 18 '21 at 08:46
  • 1
    Yes, I've tried your config. I'm starting to think the config files aren't being read at all by typescript. Very frustrating. I think I'm going to update vscode, and restart. – NicoWheat Jul 18 '21 at 08:47
  • Yes I think apart from config you have some other problem with your `IDE` – Alireza Ahmadi Jul 18 '21 at 08:49
  • I just got partly working by installing ts-node (https://github.com/TypeStrong/ts-node#usage). I can delete the tsconfig.json. `npm i -D ts-node typescript tslib @types/node` and now I can run the code no errors and the console.log() shows in command line using this command `npx ts-node src/index.ts`. Not sure if this is an answer yet, need to understand a bit more what happened. – NicoWheat Jul 18 '21 at 17:26
0

Running tsc src/idnex.ts won't pick up your tsconfig.json, as stated by the documentation.

# Emit JS for just the index.ts with the compiler defaults
tsc index.ts

(with the compiler defaults)

tsc doesn't support arguments like -p tsconfig.json src/index.ts either. There is no way to compile a single file with tsconfig.json.


Two workarounds:

  1. Specify your compiler options in command line.
  2. Create a new tsconfig.singlefile.json, put your source file in the include option. See this exact same question.
yume_chan
  • 858
  • 9
  • 22
0

Updating dependencies ts-node typescript @types/node helped me in the same situation.

No tsconfig lib update needed if the target in your tsconfig is already 2019+

Marina Zubkova
  • 95
  • 1
  • 2
  • 7