Mocha 找不到全局类型

问题描述 投票:0回答:1

我用 express 编写了小型节点应用程序,并制作了全局接口“LocalUser”,它本质上是快速响应,但预定义了局部变量。我决定创建全局类型,而不是到处导入它

declare global {
  export interface LocalUser extends express.Response {
    locals: UsersTokens;
  }
}

不知为何摩卡不喜欢发给我

TSError: ⨯ 无法编译 TypeScript: src/router/index.ts(40,44):错误 TS2304:找不到名称“LocalUser”。

重现它的最少代码:

  constructor() {
    this.app = express();
  }

    this.app.all('*', function (_req, res: LocalUser) {
      const { message, code, name, status } = new NotFoundError();
      res.status(status).json({ message, code, name });
    });

    this.server = http.createServer(this.app);

    this.server.listen(getConfig().httpPort, () => {
      console.log(`Listening on ${getConfig().httpPort}`);
    });

节点:“18.7.0” 摩卡:“^10.0.0”,

这是我第一次使用全局接口。这只是一种正常行为吗?整个应用程序正常工作,并且可以在 js 中运行,但只有 mocha 有问题

我刚刚删除了全局导出。想不通,如何正确导出全局接口

node.js typescript express mocha.js chai
1个回答
0
投票

我知道我迟到了,但其他人仍然可能像我一样结束在这里

只有当我在

global.d.ts
“文件”属性中添加
package.json
时,测试才会运行。

{
  "compilerOptions": {
    "target": "es2022",
    "module": "commonjs",
    "rootDir": "./src",
    "allowJs": true,
    "outDir": "./dist",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "suppressImplicitAnyIndexErrors": true,
    "skipLibCheck": true,
    "noImplicitAny": true,
    "types": ["node", "mocha"]
  },
  "files": ["global.d.ts"],
  "include": ["src/**/*.ts"],
  "exclude": ["./node_modules"]
}
© www.soinside.com 2019 - 2024. All rights reserved.