Typescript TS2339属性不存在

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

这是我的抓头器。无法解决这个问题……可能需要对此事重新审视

我有以下代码

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()];
                                     ~~~~~~~

这只是我的傻瓜...

我正在使用typescript 3.6.3,在node 12.8.1上运行,并使用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"]
}

node.js typescript ts-node
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.