首页 > 解决方案 > Vscode Typescript - 如何引用 types.d.ts

问题描述

我下载了screeps typescript starter并编写了代码。一切正常。然后我尝试将我的代码放在文件夹中(将“src/xx.ts”移动到“src/prototypes/xx.ts”)。VSCode 开始抛出如下错误:

'Creep'.ts(2339) 类型上不存在属性'isRemoteRole'

但是该属性是在 types.d.ts 中声明的。当我尝试使用 VScode 快速修复时,它会将属性声明添加到 index.d.ts 而不是 types.d.ts。这里有什么问题?

我试图在我的 tsconfig 中明确指定类型文件的路径并重新启动 VSCode,但这似乎没有帮助。

我的 tsconfig.json:

{
  "compilerOptions": {
    "module": "esnext",
    "lib": ["esnext"],
    "target": "es2017",
    "moduleResolution": "Node",
    "outDir": "dist",
    "baseUrl": "src/",
    "sourceMap": true,
    "strict": true,
    "experimentalDecorators": true,
    "noImplicitReturns": true,
    "allowSyntheticDefaultImports": true,
    "allowUnreachableCode": false
  },
  "exclude": ["node_modules"],
  "types": ["src/types.d.ts"]
}

标签: typescriptvisual-studio-code.d.ts

解决方案


我发现我的 types.d.ts 无效,这就是为什么没有检测到。由于某种原因,types.d.ts 顶部的导入导致它抛出一个不可见的错误。

我删除了:

import { ResourceTask } from "../screeps2/src/_models";

并将其替换为:

import("c:/Users/me/Desktop/screeps2/src/helper").ResourceTask

推荐阅读