TypeScript Getting Started
TypeScript Compiler
TypeScript is transpiled into JavaScript using a compiler.
TypeScript being converted into JavaScript means it runs anywhere that JavaScript runs!
Installing the Compiler
TypeScript has an official compiler which can be installed through npm.
Learn more about npm, and how to get started here:What is npm?
Within your npm project, run the following command to install the compiler:
Which should give you an output similar to:
found 0 vulnerabilities
The compiler is installed in thenode_modules
directory and can be run with:npx tsc
.
Which should give you an output similar to:
tsc: The TypeScript Compiler - Version 4.5.5
Followed by a list of all the Common Commands.
Configuring the compiler
By default the TypeScript compiler will print a help message when run in an empty project.
The compiler can be configured using atsconfig.json
file.
You can have TypeScript createtsconfig.json
with the recommended settings with:
Which should give you an output similar to:
TS
target: es2016
module: commonjs
strict: true
esModuleInterop: true
skipLibCheck: true
forceConsistentCasingInFileNames: true
You can learn more at https://aka.ms/tsconfig.json
Here is an example of more things you could add to thetsconfig.json
file:
"include": ["src"],
"compilerOptions": {
"outDir": "./build"
}
}
You can open the file in an editor to add those options. This will configure the TypeScript compiler to transpile TypeScript files located in thesrc/
directory of your project, into JavaScript files in thebuild/
directory.
This is one way to quickly get started with TypeScript. There are many other options available such as acreate-react-app template, anode starter project, and awebpack plugin.