Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript 'Record' is not defined

I am writing a React-Native project with TypeScript, so far everything works fine but the following part throws an error:

export interface Country {
    name: string;
    cities: Record<string, string>;
}

The error is:

3:13  error  'Record' is not defined  no-undef

My typescript uses the workspace version instead of VS Code's version. Also, when I cmd+click on the Record, I can go to its definition, which is in the file lib.es5.d.ts:

type Record<K extends keyof any, T> = {
    [P in K]: T;
};

So, Record is obviously defined and found under node_modules, however when I run the linter (@typescript-eslint), I can't fix this error

The content of my .vscode/settings.json looks like following:

"typescript.tsdk": "node_modules/typescript/lib"

I have not found any solutions, could you help please?

Typescript version: "4.1.4",

like image 376
Faruk Yazici Avatar asked Mar 17 '21 11:03

Faruk Yazici


People also ask

How do you define a record in TypeScript?

All of the values have the same format or type - that is, each has two properties: firstName and lastName . Any Record in TypeScript takes the form Record<K, T> , where K is the type of the key, and T is the type of the values. Above, we have defined a new type User for our values, and set our keys to type string .


Video Answer


1 Answers

This error might be related to the eslint configuration.

make eslint extend 'plugin:@typescript-eslint/recommended' by adding it to extends section of eslintrc file:

module.exports = {
    ...
    extends: [
        ...
        'plugin:@typescript-eslint/recommended'
    ]
    ....
};

and reload vscode's TypeScript Server. you can do so by typing Restart TS Server in vscode command palette.

like image 198
Mohammad Rahmanian Avatar answered Sep 20 '22 16:09

Mohammad Rahmanian