简体   繁体   English

打字稿和Node.JS结合

[英]Typescript and Node.JS combined

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. 我有一个以某种方式用JavaScript编写的工作的node.js服务器(不是我写的),我决定使用Typescript重写它,因为我是.NET。 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 方法a) -构建成功,但是节点无法运行

File PeripheryInstance.ts: 文件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 方法b) -成功构建,节点正在运行,但是我不能使用“ intellisense”(类型PeripheryInstance上的myMethod())并且代码更难以阅读

File PeripheryInstance.ts: 文件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? 我的问题是:是否可以将方法a)与node.js一起使用,以便可以使用所有类型代码的特权? Or I have to use some form of approach b) ? 还是我必须使用某种形式的方法b) Thank you for. 谢谢你

You'll need to install typings for node and any other dependent libraries you are using: npm install --save @types/node 您需要为节点和正在使用的任何其他依赖库npm install --save @types/nodenpm install --save @types/node

You'll also need typescript: npm install --save-dev 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/ 这是我关注的内容: 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. 除了您需要在运行输出之前设置Typescript编译之外,这没有什么其他的。 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. 不要在代码中的any地方使用any类型,因为那样会扼杀使用Typescript的目的,并且您将不会拥有Intellisense。 Instead use the proper types for each method and class. 而是为每个方法和类使用正确的类型。

In approach A, what do you mean by Node can't run it? 在方法A中,Node无法运行意味着什么? You should run the generated output after build. 您应该在构建后运行生成的输出。 Not the typescript files, but the JS ones. 不是打字稿文件,而是JS文件。

In approach B, there are some mistakes. 在方法B中,存在一些错误。 You should not do module.exports . 您不应该执行module.exports The right way is export class PeripheryInstance{} , for example. 正确的方法是export class PeripheryInstance{} Also, require is not the right way to use in Typescript. 另外, require不是在Typescript中使用的正确方法。 Use the import syntax instead. 请改用import语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM