TypeScript中的模块解析如何用于全局值(即描述)?

soo*_*sap 13 mocha.js typescript jestjs cypress

在单独使用jest时,安装后即可检测到相应的打字稿定义@types/jest

然后,我开始使用cypress进行集成测试。由于cypress使用的是mocha,因此我现在在我的jest测试中错误地看到了mocha类型定义的引用。实际上,检测到许多重叠的类型定义。例如,describe似乎在许多文件中定义了。我什至尝试实现自己的describe指向笑话的类型。不幸的是,摩卡咖啡每次都“获胜”。

当打字稿编译器检测到多个定义时,如何指定优先顺序?

多个打字稿定义用于描述

我的tsconfig.json样子是这样的:

{
    "compilerOptions": {
        "target": "es5",
        "lib": [ "dom", "dom.iterable", "esnext" ],
        "types": [ "jest", "mocha" ],
        "allowJs": true,
        "skipLibCheck": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "strict": true,
        "strictNullChecks": true,
        "forceConsistentCasingInFileNames": true,
        "module": "esnext",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "isolatedModules": false,
        "noEmit": true,
        "jsx": "preserve"
    },
    "include": [ "src/**/*" ]
}
Run Code Online (Sandbox Code Playgroud)

但是,我也尝试了以下方法:

{
    "compilerOptions": {
        "target": "es5",
        "lib": [ "dom", "dom.iterable", "esnext" ],
        "typeRoots": [ "./node_modules/@types", "./src/types" ],
        "allowJs": true,
        "skipLibCheck": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "strict": true,
        "strictNullChecks": true,
        "forceConsistentCasingInFileNames": true,
        "module": "esnext",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "isolatedModules": false,
        "noEmit": true,
        "jsx": "preserve"
    },
    "include": [ "src/**/*" ]
}
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,都选择了摩卡咖啡。如何切换“描述与合作”的类型。以开玩笑

小智 2

CompilerOptions.types 允许您限制您希望在范围(文件夹)中可用的类型

您可以尝试以下操作:使用 CompilerOptions.types = [] 创建顶级 tsconfig.json

在测试文件夹内创建 tsconfig.json 并选择 jesttypings CompilerOptions.types = ['jest']

同样在集成文件夹中,创建 tsconfig.json 并选择 mochatypings CompilerOptions.types = ['mocha']