Node.js Expressjs和TypeScript-无法键入express

Node.js Expressjs和TypeScript-无法键入express,node.js,typescript,express,Node.js,Typescript,Express,我正在尝试使用express和TypeScript正确地键入nodejs应用程序。考虑这个代码: import express, { Router } from "express"; import { DeviceController } from "../controller/device.controller"; export class DeviceRouter { public router: Router; private deviceController: DeviceC

我正在尝试使用express和TypeScript正确地键入nodejs应用程序。考虑这个代码:

import express, { Router } from "express";
import { DeviceController } from "../controller/device.controller";

export class DeviceRouter {
  public router: Router;

  private deviceController: DeviceController;

  constructor() {
    this.deviceController = new DeviceController();
    this.router = express.Router();

    this.router.get("/", this.deviceController.index);
    this.router.post("/", this.deviceController.create);
    this.router.delete("/:id", this.deviceController.delete);
  }
}
这会导致错误

TSError: ⨯ Unable to compile TypeScript:
src/module/device/router/device.router.ts:13:17 - error TS2339: Property 'get' does not exist on type 'Router'.

13     this.router.get("/", this.deviceController.index);
                   ~~~
src/module/device/router/device.router.ts:14:17 - error TS2339: Property 'post' does not exist on type 'Router'.

14     this.router.post("/", this.deviceController.create);
                   ~~~~
src/module/device/router/device.router.ts:15:17 - error TS2339: Property 'delete' does not exist on type 'Router'.

15     this.router.delete("/:id", this.deviceController.delete);
               ~~~~~~
当然,当路由器被输入为
any
时,一切都正常工作。我在键入
app
时也有同样的问题,首先是在一切正常时编写示例代码:

import express from "express";

class App {
  public app: any;

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

export default new App().app;
例如,当键入导致错误时:

import express, { Application } from "express";

class App {
  public app: Application;

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

export default new App().app;
结果:

src/server.ts:5:5 - error TS2339: Property 'listen' does not exist on type 'Application'.

5 app.listen(PORT, () => console.log(`App listening on port ${PORT}!`));
我的配置:

macOS Catalina 10.15.2 node.js v10.16.0

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es6",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "paths": {
      "*": ["node_modules/*"]
    }
  },
  "include": ["src/**/*"]
}
相关依赖项:

  "dependencies": {
    "express": "~4.17.1"
  },
  "devDependencies": {
    "@types/express": "~4.17.2",
    "@types/node": "~13.5.0",
    "ts-node": "~8.6.2",
    "typescript": "~3.7.5"
  }

如何运行您的服务器<代码>ts节点..你的.ts对吗?因为我一直在尝试你的代码,它运行得很好。我正在通过nodemon运行它,但我试图直接通过ts节点运行它,同样的错误会弹出。此外,我现在在arch linux上试用了它,它完全相同,因此它不依赖于平台。我使用rushjs作为monorepo管理器,使用pnpm作为包管理器。当我使用npm时,一切都按预期工作。所以我认为它与typescript和express类型无关,而是与rush或pnpm本身有关。