【发布时间】: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