代码之家  ›  专栏  ›  技术社区  ›  PlayMa256

vscode找不到typeRoots类型定义

  •  0
  • PlayMa256  · 技术社区  · 6 年前

    我尝试只使用一个文件来保存将在整个应用程序中使用的大多数类型定义,我创建了一个名为 @types 和一个 index.d.ts 导出我想要的每个接口/类型的文件。

    更新了我的tsconfig.json文件要包含该文件夹:

    {
        "compilerOptions": {
            "outDir": "./built",
            "allowJs": true,
            "target": "es5",
            "jsx": "react",
            "allowSyntheticDefaultImports": true,
            "lib": [
                "es2015"
            ],
            "typeRoots": [
                "./@types"
            ],
        },
        "include": [
            "./client/**/*",
            "./@types/**/*"
        ],
        "awesomeTypescriptLoaderOptions": {
            "silent": true,
            "errorsAsWarnings": true
        }
    }
    

    即使在代码中(在client/…)我引用index.d.ts上的一个接口时,vscode抛出一个“找不到名称”。

    有没有更好的方法来解决这个问题?

    @types/index.d.ts中存在接口定义。

    export interface Agent {
        name: string;
    ...
    }
    

    interface Props extends RouteComponentProps<any> {
        agent?: types.Agent;
    }
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   Matt McCutchen    6 年前

    既然你用的是 export 关键字, @types/index.d.ts 被视为一个模块,从中访问类型的唯一方法是导入它们。例如,在完成以下操作之后,您可以参考 types.Agent

    import * as types from "../path/to/@types/index";
    

    您可能希望删除所有出现的 出口 以便 Agent . 或者您可以将类型放在一个名为 types 然后参考 类型。代理

    这个 typeRoots 编译器选项不相关。它告诉编译器在哪里可以找到包含应该加载的声明的包,但是您的问题不是加载声明(因为 与您的 include