What is declare var in Node.js?

后端 未结 1 1508
心在旅途
心在旅途 2021-01-06 01:23

in this nodejs code,

declare var process: NodeJS.Process;
declare var global: NodeJS.Global;
declare var console: Console; 
declare var __filename: string;
d         


        
1条回答
  •  有刺的猬
    2021-01-06 01:37

    When you use:

    var process: NodeJS.Process;
    

    You are creating a variable named process (with no value defined) and telling the TypeScript compiler to enforce the NodeJS.Process type for assignments.

    When you add declare:

    declare var process: NodeJS.Process;
    

    You are telling the TypeScript compiler that there is already a variable named process with the type NodeJS.Process. This is useful when you have variables introduced by sources that the compiler is not be aware of.

    See Declaration Files in the TypeScript handbook.

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