How to generate TypeScript definition file from any .js file?
A TypeScript definition file or a type declaration file is any TypeScript file that has the .d.ts filename extension. These files are only meant to hold the type declarations of that particular script and not the source code itself. This means that they are not a part of the compilation process. In this article, we will see how to generate a TypeScript definition file from any .js file or any normal TypeScript file.
We enter the following command into the terminal to generate any TypeScript definition file from a .js file:
tsc --declaration nameOfTypeScriptFile.ts
- tsc: It stands for TypeScript compiler which is used to invoke the compiler.
- –declaration: It is a Command Line Interface (CLI) command which emits the TypeScript definition file from the TypeScript (.ts) file.
- nameOfTypeScriptFile.ts: The TypeScript file which the TypeScript definition file is to be generated from.
Example 1: The following example has an object literal with two key-value pairs, the first property being name with its corresponding value as “GeeksforGeeks” and the second property being founded with its value being 2009.
script.ts: Generated output TypeScript file
- script.d.ts: This is Generated TypeScript definition file
Example 2: The following example has a user-defined function addTwoNumbers(a,b) which computes the sum of two numbers and returns the result.
Please Login to comment...