Compiling TypeScript to JavaScript
The most common way to run TypeScript code is to compile it to JavaScript first. You can do this using the TypeScript compilertsc.
Install TypeScript locally
In this example we’re going to use npm. You can check our introduction to the npm package manager for more information.
Compile your TypeScript code
Compile your TypeScript code to JavaScript using the
tsc command:npx is a tool that allows you to run Node.js packages without installing them globally.tsc is the TypeScript compiler which will take our TypeScript code and compile it to JavaScript. This command will result in a new file named example.js that we can run using Node.js.If there are type errors
If you have type errors in your TypeScript code, the TypeScript compiler will catch them and prevent you from running the code. For example, if you change theage property of justine to a string, TypeScript will throw an error.
We will modify our code like this, to voluntarily introduce a type error: