I have a working node.js server somehow written in javascript (that is not written by me) and I've decided to rewrite it using typescript, because I'm .NET guy. Is there any way to use it with node and preserve types at the same time?
Approach a) - successful build, but node can't run it
File PeripheryInstance.ts:
class PeripheryInstance {
Type: string;
PortName: string;
constructor(type: string, portName: string) {
this.Type = type;
this.PortName = portName;
}
myMethod(){
}
}
File Server.ts
class Server{
static periphery: PeripheryInstance;
public static start() {
this.periphery = new PeripheryInstances("a", "b");
this.periphery.myMethod();
}
}
Approach b) - successful build, node is running, but I can't use "intellisense" (myMethod() on type PeripheryInstance) and the code is more difficult to read
File PeripheryInstance.ts:
module.exports = class PeripheryInstance {
Type: string;
PortName: string;
constructor(type: string, portName: string) {
this.Type = type;
this.PortName = portName;
}
myMethod(){
}
}
File Server.ts
var pi = require('./PeripheryInstance');
class Server{
// pi.PeripheryInstance return (TS) cannot find namespace pi
static periphery: any;
public static start() {
this.periphery = new pi.PeripheryInstances("a", "b");
// myMethod is not suggested by intellisence, because this.periphery is "any"
this.periphery.myMethod();
}
}
My question is: Is there any way to use approach a) with node.js so I can use all perks of typed code? Or I have to use some form of approach b)? Thank you for.