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)?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With