Correct way to reference modules from other modules in Typescript

前端 未结 1 1549
我在风中等你
我在风中等你 2021-01-14 05:43

I\'m writing a module for NodeJS in Typescript. I\'m trying to process a request (which should be an IncomingMessage object) using this module.



        
相关标签:
1条回答
  • 2021-01-14 06:17

    If you are writing a module, nothing you write is in the global scope - the file itself is a module, and everything inside it is scoped to that module.

    import http = require('http');
    
    export function processRequest(req : http.IncomingMessage) : boolean {
        return false;
    };
    

    In the example above, the file, rateLimiter.ts is the module. http is imported into the rateLimiter module.

    You need to compile with the module flag - for example:

    tsc --module commonjs rateLimiter.ts
    

    Most editors and IDEs supply a way to set this too.

    0 讨论(0)
提交回复
热议问题