Reputation: 994
this one is head scratcher for me. Can't figure this one out... probably need some fresh eye on the matter
I've got the following code
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;
}
}
And I keep getting the same errors, which to me makes no sense at all...
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()];
~~~~~~~
This is just bonkers to me...
I'm using typescript 3.6.3
, running on node 12.8.1
and using ts-node 8.4.1
to plugin TS support
I've pasted the code on type the whole code on TS playground. Made some changes to remove the imports and undefined functions, but overall the above errors do not appear, so I'm kind stomped... if anyone would be kind to point me in the direction of solving this problem that would be fantastic :)
Also, here is my 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"]
}
Upvotes: 4
Views: 3535
Reputation: 86
I also had this issue and spent a long time trying to figure out what was wrong. I'm going to leave this here for people who might run into this in the future. It turns out I had a ts-node re-register as part of my code to get a Typescript Worker thread working. If you have this issue, double check if you using ts-node in the code.
const path = require("path");
require("ts-node").register(); // <-- BAD
require(path.resolve(__dirname, "./worker.ts"));
Upvotes: -1
Reputation: 994
I've finally figured out what has happened. A clue was that the errors reported has been reporting errors on an already transpiled code. Typescript was ran against a JS code...
What the issue was I was starting the server using nodemon
and requiring ts-node
again manually. This was the command I've been using
nodemon --ext ts --delay 100ms -r ts-node/register src/app.ts
And it was fine (and even needed) when on my previous project which used nodemon 1.18.9
.
But, when I was setting up a new project I've automatically upgraded nodemon
to version 1.19.2
and as one can check in nodemon v1.19.0 release notes the TS has been added to default exec path. So, once the nodemon
figures out you are running TS files it will change the final node
command
FROM
node -r ts-node/register src/app.ts
TO
ts-node -r ts-node/register src/app.ts
Which means that the Typescript will be ran twice and the second time it will ran against an already finished transpiled code a.k.a JS code.
Anyways, once I've changed the start-up command to
nodemon --ext ts --delay 100ms src/app.ts
it all worked as expected.
Hope this helps someone should anyone ran into a similar problem
Upvotes: 2