【问题标题】:Visual studio 2017 | TS2304: Cannot find name 'unknown'视觉工作室 2017 | TS2304:找不到名称“未知”
【发布时间】:2019-04-27 12:00:26
【问题描述】:

我尝试将一些新的类型定义文件加载到我的 cordova/typescript 项目中。

现在我收到以下错误:

TS2304:找不到名称“未知”。

在这些定义文件中,unknown type (keyword) 不像“任何”或“字符串”等那样涂成蓝色。

Manually installing typescript extension也没有解决。

tsconfig.json:

{
  "compileOnSave": true,
  "compilerOptions": {
    "inlineSources": true,
    "module": "system",
    "noEmitOnError": true,
    "noImplicitAny": false,
    "out": "www/scripts/appBundle.js",
    "preserveConstEnums": true,
    "removeComments": true,
    "sourceMap": true,
    "target": "es2015"
  },
  "files": [ ... ]
}

VS2017 信息:

Microsoft Visual Studio Community 2017 版本 15.9.2 VisualStudio.15.Release/15.9.2+28307.108 Microsoft .NET 框架 版本 4.7.03056

安装版本:社区

TypeScript 工具 15.9.20918.2001 适用于 Microsoft 的 TypeScript 工具 视觉工作室

适用于 Apache Cordova 15.123.7408.1 的 Visual Studio 工具

Visual Studio 命令提示符输出:

【问题讨论】:

  • 听起来好像您使用的是旧版本的 Typescript,因此编译器无法识别 TS 3.0 中引入的 unknown 关键字。你能检查一下你的 Typescript 版本吗like so
  • @pushkin - 查看已编辑的答案
  • @AndyJ 我已经右键单击该项目,然后单击属性,但没有“TypeScript Build”选项卡...
  • 会检查并通知您

标签: typescript


【解决方案1】:

unknown 已添加到 TS 3.0 中,因为您收到此错误,这意味着您的项目 (package.json) 的版本较低,而 VSC 有支持它的 3.1.2 并且不显示错误。

最佳做法是在您的 IDE 和项目中使用相同版本的 TS。

如果保留 TS 3.0 对您很重要,您可以添加到项目的声明中,如果您没有它们,请使用 index.ts。

declare type unknown = any;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-12
    • 2017-04-25
    • 2018-12-03
    • 2018-09-04
    • 1970-01-01
    • 2019-12-03
    • 2017-08-08
    • 2018-01-30
    相关资源
    最近更新 更多