【问题标题】:TypeScript 'Record' is not definedTypeScript“记录”未定义
【发布时间】:2021-06-14 17:37:56
【问题描述】:

我正在使用 TypeScript 编写一个 React-Native 项目,到目前为止一切正常,但以下部分会引发错误:

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

错误是:

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

我的打字稿使用工作区版本而不是 VS Code 的版本。 另外,当我 cmd+单击Record 时,我可以转到它的定义,它在文件lib.es5.d.ts 中:

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

所以,Record 显然是在 node_modules 下定义和找到的,但是当我运行 linter (@typescript-eslint) 时,我无法修复此错误

我的.vscode/settings.json 的内容如下所示:

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

我还没有找到任何解决方案,请你帮忙吗?

打字稿版本:“4.1.4”,

【问题讨论】:

  • 它可能在编译器标志中缺少 es5(或兼容的)lib (tsconfig.json compilierOptions => lib)

标签: javascript typescript react-native visual-studio-code


【解决方案1】:

此错误可能与eslint 配置有关。

通过将 eslint 添加到 eslintrc 文件的 extends 部分来扩展 'plugin:@typescript-eslint/recommended'

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

并重新加载 vscode 的 TypeScript Server。你可以通过在 vscode 命令面板中输入Restart TS Server 来实现。

【讨论】:

    猜你喜欢
    • 2014-09-03
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    • 2017-12-29
    • 2016-12-14
    • 2017-09-14
    相关资源
    最近更新 更多