If I use a typescript tuple in my react-app i get a eslint error on line 1 in vscode?

Viewed 1551

The error is Parsing error: Cannot read property 'map' of undefined

I set up a new file just to reproduce the error:

export default () => {
  let something: [string, boolean]
  something = ['something', true];
}

am i missing something? all other types seem to work fine.

3 Answers

The Problem

The problem is when you created the tuple type [string, boolean] this is a new way to declare types with arrays introduced in Typescript 4.0 called Labeled Tuple Types.

The Solution

Upgrade your react-scripts to v4 (Here is the issue on their github). If you are using yarn use the command yarn upgrade react-scripts@4.0.1. With npm should be something similar, you can try npm install react-scripts@4.0.1

If i right understand you, here is your solution (you don't have to create a let in order to register the types, but directly in the function specify which types it will return)

// main.ts
export default (): [string, boolean] => {
 return ["something", true];
};

And if you need to call this code, you just create such a component(example):

import foo from "./main.ts"

const App = () => {
  return (
    <div>
      {foo().map(el =>
        <div>
          {el}
        </div>
      )}
    </div>
  )
};
Related