6

in this nodejs code,

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

that...

What's the difference between 'declare var' and 'var'?

When I look up on the googling, I get the word runtime.

wiki says runtime is an operation while a computer program is running....

but i can't understand.

and line 1, what does it mean by ":" after "process" and then "NodeJS.Process"?

Is that mean "process" is equal "NodeJS.Process"?

also line 4, what does it mean by ":" after "__filename" and then "string"?

Is that mean "__filename" is equal "string"?

thanks you.

Ry-
  • 218,210
  • 55
  • 464
  • 476
ONION
  • 239
  • 1
  • 4
  • 13
  • `in this nodejs code` what are you actually looking at? got a link? – Jaromanda X Apr 10 '18 at 05:21
  • 7
    Are you using some kind of preprocessor that does static type checking? That looks more like TypeScript. – 4castle Apr 10 '18 at 05:22
  • 1
    in TypeScript, the part after `:` declares the variables **type** – Jaromanda X Apr 10 '18 at 05:26
  • to Jaromanda X. there is https://github.com/IoTKETI/Mobius/// i download this and open VS207. and In the file mobius.js, line 22, there is "global". so i use definition picking then I could see those codes. – ONION Apr 10 '18 at 05:26
  • from ONION ... what? – Jaromanda X Apr 10 '18 at 05:27
  • The snippet is from a [TypeScript declaration file](https://stackoverflow.com/questions/21247278/about-d-ts-in-typescript). See: [DefinitelyTyped's node/index.d.ts](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/e36116f93c495cc2af65af64217b375a3e39cdbf/types/node/index.d.ts#L90) – Jonathan Lonowski Apr 10 '18 at 05:28
  • check this thread would help https://stackoverflow.com/questions/35019987/what-does-declare-do-in-export-declare-class-actions – Anil Namde Apr 10 '18 at 05:30
  • Thank you all for the answers! – ONION Apr 10 '18 at 05:32

1 Answers1

12

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.

Fenton
  • 241,084
  • 71
  • 387
  • 401