Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript error: Property 'flat' does not exist on type '[string, unknown][]'

I need to use .flat() on an array, but Typescript claims:

Property 'flat' does not exist on type '[string, unknown][]'

I've added "es2019 to my tsconfig file:

    "lib": ["es2015", "es2019", "dom"],

The function where flat() is used is:

const legendSeries = (series) => {
    const formattedSeries = {};
    series.map((serie) =>
      Object.entries(serie).map((s) => {
        if (s[0] in formattedSeries) {
          return (formattedSeries[s[0]].count += s[1]);
        }
        return (formattedSeries[s[0]] = { id: s[0], label: s[0], count: s[1] });
      })
    );
    return Object.entries(formattedSeries)
      .flat()
      .filter((_, i) => i % 2 !== 0);
  };

The error persists. How to fix this (it's a React project in case it might be helpful)?

like image 641
DoneDeal0 Avatar asked Jun 11 '20 12:06

DoneDeal0


Video Answer


1 Answers

I think this issue is because typescript doesn't have access to array.flat(), in such a case it will throw an error of not have knowledge of flat(), as mentioned above by @codejockie. You need to update your config file like { "compilerOptions": { "target": "es5", "lib": [ "es2019" ] } }

we need to add es2019 or es2019.array to --lib setting for TypeScript so that it starts recognizing array.flat() and flatMap().

After making the changes. Please restart your project if the changes don't reflect.

like image 110
Shubhra Kushal Avatar answered Oct 16 '22 09:10

Shubhra Kushal