【问题标题】:Typescript 3+ features not working even though Typescript 3+ version is installed即使安装了 Typescript 3+ 版本,Typescript 3+ 功能也不起作用
【发布时间】:2020-05-06 22:22:37
【问题描述】:

我已全局安装了 typescript 3.7.3,Visual Code 使用的是 Typescript 3.7.3 版,但我似乎无法访问 Typescript 3+ 功能。例如,当尝试声明类型为“未知”的变量时,我收到一个编译错误:“找不到名称‘未知’。尽管它似乎可以作为智能建议使用。我该如何解决这个问题?

我的 tscongif.json 是:

{
 "compileOnSave": false,
 "compilerOptions": {
   "baseUrl": "./",
   "outDir": "./dist/out-tsc",
   "sourceMap": true,
   "declaration": false,
   "downlevelIteration": true,
   "emitDecoratorMetadata": true,   // Needed for Stackblitz
   "experimentalDecorators": true,
   "module": "esnext",
   "moduleResolution": "node",
   "importHelpers": true,
   "target": "es2015",
   "typeRoots": [
     "node_modules/@types"
   ],
   "lib": [
     "es2018",
     "dom"
   ]
 },
 "angularCompilerOptions": {
   "fullTemplateTypeCheck": true,
   "strictInjectionParameters": true
 }
}

【问题讨论】:

  • 如果您通过命令行构建,您是否也会看到此错误?还是仅在 Visual Studio Code 中?
  • 它只在 VS 代码中。并且智能感知知道类型并提供自动完成,但是一旦添加它就会被标记为红色,并带有错误代码,如图所示。

标签: angular typescript visual-studio-code compiler-errors tsconfig


【解决方案1】:

一个版本的 TypeScript 也可能安装到您的 node-modules 文件夹中。

tsc -v 正在报告全局 安装的版本。 Visual Studio Code 也将拥有自己的 TypeScript 编译器版本。

/node-modules/typescript/package.json 中检查将要使用的 TypeScript 的实际 { "version": "X.X.X" }

您的项目package.json 文件将定义:

"devDependencies": {
    ...
    "typescript": "X.X.X" <- update this.
  }

【讨论】:

  • 谢谢,我已经检查过了。我现在已经全局安装了 3.4.3 版本并设置了 VS Code 以使用它,并且在我的 package.json 中我有“typescript”:“^3.4.3”。我仍然得到:“找不到名称'未知'”。
  • 还有什么可以超越它的吗?如果我理解正确的话,有一个 VS 代码使用的打字稿语言服务,正如你提到的 node_modules 中的打字稿文件一样:在 settings.json 我有 "typescript.tsdk": "node_modules\\typescript\\lib"
猜你喜欢
  • 2021-07-02
  • 2019-06-20
  • 2019-07-03
  • 2016-09-20
  • 2013-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多