打字稿 TS2339 属性 不存在

Typescript TS2339 property does not exist

这个让我很头疼。想不通这个……可能需要重新审视这个问题

我有以下代码

import express from 'express';
import { isFunction } from 'lodash';

export class Server {
  private _server = express();
  private _namespace = '/api/v1';
  public constructor(private _port: number) {}

  public addRoute({ path, handler, method }): this {
    var requestHandler = this._server[String(method).toLowerCase()];
    if (false === isFunction(requestHandler)) throw new Error('Invalid HTTP method');
    requestHandler(path, handler);
    return this;
  }
}

而且我不断收到相同的错误,这对我来说毫无意义...

TSError: ⨯ Unable to compile TypeScript:
src/server/main.ts:21:14 - error TS2339: Property '_port' does not exist on type 'Server'.

21         this._port = _port;
                ~~~~~
src/server/main.ts:22:14 - error TS2339: Property '_server' does not exist on type 'Server'.

22         this._server = express_1.default();
                ~~~~~~~
src/server/main.ts:23:14 - error TS2339: Property '_namespace' does not exist on type 'Server'.

23         this._namespace = '/api/v1';
                ~~~~~~~~~~
src/server/main.ts:34:35 - error TS2339: Property '_server' does not exist on type 'Server'.

34         var requestHandler = this._server[String(method).toLowerCase()];
                                     ~~~~~~~

这对我来说太疯狂了...

我在 node 12.8.1 上使用 typescript 3.6.3, 运行 并使用ts-node 8.4.1 插件 TS 支持

我已经在 TS playground 上粘贴了键入整个代码的代码。进行了一些更改以删除导入和未定义的函数,但总的来说没有出现上述错误,所以我很沮丧......如果有人愿意指出我解决这个问题的方向,那就太棒了:)

此外,这是我的 tsconfig.json

{
  "compilerOptions": {
    "noImplicitThis": false,
    "rootDir": "src",
    "typeRoots": ["node_modules/@types", "@types"],
    "lib": ["es6"],
    "strict": true,
    "strictPropertyInitialization": false,
    "strictFunctionTypes": true,
    "esModuleInterop": true,
    "target": "es6",
    "noImplicitAny": false,
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": false,
    "pretty": true,
    "outDir": "build",
    "alwaysStrict": false,
    "noImplicitReturns": true,
    "noStrictGenericChecks": true,
    "noUnusedLocals": true,
    "noUnusedParameters": false,
    "suppressImplicitAnyIndexErrors": true,
    "preserveConstEnums": false,
    "strictNullChecks": true,
    "allowSyntheticDefaultImports": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "include": ["./**/*.ts"],
  "compileOnSave": true,
  "exclude": ["node_modules"]
}

我终于明白是怎么回事了。一个线索是报告的错误一直在报告已经 t运行spiled 代码的错误。打字稿是 运行 针对 JS 代码...

问题是我使用 nodemon 启动服务器并再次手动要求 ts-node。这是我一直在使用的命令

nodemon --ext ts --delay 100ms -r ts-node/register src/app.ts 

在我之前使用 nodemon 1.18.9.

的项目中,它很好(甚至需要)

但是,当我设置一个新项目时,我已经自动将 nodemon 升级到版本 1.19.2 并且可以检查 in nodemon v1.19.0 release notes TS 已添加到默认 exec小路。因此,一旦 nodemon 确定您是 运行 TS 文件,它将更改最终的 node 命令

node -r ts-node/register src/app.ts

ts-node -r ts-node/register src/app.ts

这意味着 Typescript 将 运行 两次,第二次它将 运行 针对已经完成的 t运行spiled 代码 a.k.a JS 代码。

无论如何,一旦我将启动命令更改为

nodemon --ext ts --delay 100ms src/app.ts 

一切如期进行。

希望这可以帮助任何人 运行 遇到类似的问题

我也有这个问题,花了很长时间试图找出问题所在。我将把它留给将来可能 运行 的人。事实证明,我有一个 ts-node re-register 作为代码的一部分来让 Typescript Worker 线程工作。如果您遇到此问题,请仔细检查您是否在代码中使用了 ts-node。

const path = require("path");
require("ts-node").register(); // <-- BAD
require(path.resolve(__dirname, "./worker.ts"));