0

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.

1 Answer 1

1

You'll need to install typings for node and any other dependent libraries you are using: npm install --save @types/node

You'll also need typescript: npm install --save-dev typescript

And then there are a lot of tutorials to get it done the right way. This is the one I followed: https://blog.risingstack.com/building-a-node-js-app-with-typescript-tutorial/

It's nothing out of the world, except that you need to set up Typescript compilation before you run the output. Don't use any type anywhere in your code since that'll kill the purpose of using Typescript and you won't have Intellisense to go with it. Instead use the proper types for each method and class.

In approach A, what do you mean by Node can't run it? You should run the generated output after build. Not the typescript files, but the JS ones.

In approach B, there are some mistakes. You should not do module.exports. The right way is export class PeripheryInstance{}, for example. Also, require is not the right way to use in Typescript. Use the import syntax instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.