摩卡测试无法识别* .d.ts声明

时间:2019-10-26 14:51:53

标签: node.js typescript mocha

我在配置Mocha以支持我的打字稿代码时遇到问题。我在项目根目录的global.d.ts文件夹中的src中定义了一些类型。

我运行mocha -r ts-node/register test/**/*.ts时收到以下错误:

TSError: ⨯ Unable to compile TypeScript:
src/mediator/index.ts:5:28 - error TS2304: Cannot find name 'DomainEvent'.

5     [key: string]: (event: DomainEvent) => Promise<void>
                             ~~~~~~~~~~~
src/mediator/index.ts:11:57 - error TS2304: Cannot find name 'DomainEvent'.

11     registerHandler(eventType: string, handler: (event: DomainEvent) => Promise<void>) {
                                                           ~~~~~~~~~~~
src/mediator/index.ts:18:37 - error TS2304: Cannot find name 'IntegrationEvent'.

18     async publish(integrationEvent: IntegrationEvent) {

当我运行ts-node ./src/app.ts时,应用运行良好。有人知道我在做什么错吗?

我的tsconfig(我怀疑问题与此有关):

{
    "compilerOptions": {
        "module": "commonjs",
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "target": "es6",
        "noImplicitAny": false,
        "moduleResolution": "node",
        "sourceMap": true,
        "outDir": "dist",
        "baseUrl": ".",
        "paths": {
            "@mediator": ["src/mediator"],
            "*": [
                "node_modules/*"
            ]
        },
        "typeRoots": [
            "./global"
        ]
    },
    "include": [
        "**/*.ts"
    ]
}

1 个答案:

答案 0 :(得分:0)

global.d.ts中加入您的tsconfig.json

{
  "compilerOptions": {
  },
  "include": [
    "./global.d.ts",
    "./src/**/*.d.ts"
  ]
}