【问题标题】:Typescript TS2339 property does not existTypescript TS2339 属性不存在
【发布时间】:2022-01-31 02:05:46
【问题描述】:

这个让我头疼。无法弄清楚这一点...可能需要对此事有新的看法

我有以下代码

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】:

    我终于弄清楚发生了什么。一个线索是,报告的错误一直在报告已转译代码的错误。 Typescript 是针对 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 已添加到默认执行路径中。因此,一旦 nodemon 发现您正在运行 TS 文件,它将更改最终的 node 命令

    来自

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

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

    这意味着 Typescript 将运行两次,第二次将针对已完成的转译代码(即 JS 代码)运行。

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

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

    一切都按预期进行。

    希望这对遇到类似问题的人有所帮助

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2017-12-31
        • 2016-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-14
        • 2021-03-24
        • 2018-03-25
        相关资源
        最近更新 更多