To install Typescript, we can use the Node Package Manager (npm), which is the package manager for JavaScript and TypeScript.
if Node hasn't been installed on our machine, we can download it by going through the below tutorials.
Node Installations Steps For Windows Click here
Node Installations Steps For Mac Click here
This will also install npm, the Node Package Manager.
Open your terminal or command prompt. The commands for installation may vary slightly based on your operating system.
// It will display the version of node.js installed on your operating system. node -v or node --version // It will display the version of npm installed on your operating system. npm -v or npm --version
npm install -g typescript
Run the following command to install Typescript globally on your system:
// it will install module under devDependency object in package.json npm install typescript --save-dev
// `latest` keyword indicate Newer version of Typescript npm install typescript@latest -g
tsc --version
To verify that Typescript has been successfully installed, we can check its version using:
tsc --init
{ "compilerOptions": { /* Language and Environment */ "target": "es2016", /* Modules */ "module": "commonjs", /* Specify what module code is generated. */ /* Interop Constraints */ "esModuleInterop": true, "forceConsistentCasingInFileNames": true, /* Type Checking */ "strict": true, /* Enable all strict type-checking options. */ /* Completeness */ "skipLibCheck": true /* Skip type checking all .d.ts files. */ } }
This should print the version number of the Typescript compiler.
tsc
While not strictly necessary, in a Typescript project, we often have a "tsconfig.json" file that specifies compiler options and settings for your project. We can generate one using:
The above command will generate the tsconfig.json file with some pre-configuration compiler options.
This command creates a basic "tsconfig.json" file in our project directory. We can customize it based on your project's needs.
To compile using tsconfig.json, run below command:
Above will read the configuration from tsconfig.json and compile the Typescript files accordingly.