TypeScript Ambients Declarations
Ambient declarations files need to save with the extension (d.ts). A file with extension .d.ts must have the declare keyword prefixed to each root level definition. It makes clear to the author that there will be no code emitted by TypeScript. The author needs to ensure that the declared item will exist at runtime.
Ambient declarations tell the compiler about the actual source code exist elsewhere. If these source codes do not exist at runtime and we try to use them, then it will break without warning.
Ambient declarations files are like docs file. If the source changes, the docs need to be kept updated also. If the ambient declaration file is not updated, it returns compilation errors.
We can declare the ambient variables and methods by using the declare keyword. The syntax for the ambient declaration is like below.
Syntax to access ambient files
Above is a JS file and we have not much time to re-write this library to TypeScript. But still, if we need to use the doSum() function with type safety, then we can do this by using ambient declaration. Let us create an ambient declaration file.
Now, include this ambient declaration file (CalcSum.d.ts) into our TypeScript file.
Compile and executed the Main.ts file by using the following command on the console.
We will get the following output.