gpt4 book ai didi

node.js - Typescript TS2339 属性不存在

转载 作者:太空宇宙 更新时间:2023-11-04 01:24:24 26 4
gpt4 key购买 nike

这对我来说是一件令人头疼的事。无法弄清楚这个问题...可能需要对此事进行一些新的审视

我有以下代码

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"]
}

最佳答案

我终于明白发生了什么了。一个线索是报告的错误一直在报告已经转译的代码上的错误。 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 

一切都按预期进行。

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

关于node.js - Typescript TS2339 属性不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58086960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com