You will need to install the TypeScript engine for you to use TypeScript. This is because your TypeScript code must be converted into JavaScript, so it needs a program called a transpiler to do the conversion to JavaScript so that a browser can run it.
What does a Transpiler Actually Do?
A transpiler is not a compiler or interpreter. It serves a similar purpose in some ways, but not in others. Here’s what a TypeScript transpiler does:
- Transpiles TypeScript to JavaScript: It takes TypeScript code, which may include modern JavaScript features, type annotations, and other TypeScript-specific syntax, and converts it into plain JavaScript. This output can be configured to target different versions of JavaScript (e.g., ES5, ES6).
- Type Checking: Before or during the transpilation process, the TypeScript transpiler checks the types in the code to ensure that variables, functions, and other elements are used according to their specified types. This helps catch potential errors during development.
- Source Maps: A TypeScript transpiler can also generate source maps, which link the compiled JavaScript code back to the original TypeScript code. This makes debugging easier, as developers can view and step through the TypeScript code in their browser’s developer tools instead of the transpiled JavaScript.
- Compatibility Adjustments: The transpiler can handle modern JavaScript features (e.g.,
async/await
, modules, etc.) and convert them to older JavaScript versions if needed, ensuring broader compatibility across different browsers or environments.
In essence, a TypeScript transpiler acts as a bridge that allows developers to write type-safe, modern code while ensuring it runs in environments that only support JavaScript.
Installing TypeScript Compiler (tsc)
Despite it not really being a compiler, it is actually called the TypeScript compiler.
Generally the best way to install this app through a command called nvm, or Node Version Manager, part of Node.JS. So you will need to install Node.JS to use typescript.
Once you have done that, you will use a command line prompt to install it so TypeScript is available globally.
npm install -g typescript
You will see TypeScript install. Check the messages to see if it was successful, or if it failed, and you need to make changes.
To learn more about installing TypeScript go to: https://www.typescriptlang.org/download/
Writing Your First TypeScript App
Once you install TypeScript Compiler, you will need to write your first app. As always, this will be a “hello world” style app.
console.log("Hello World");
If this looks a lot like JavaScript, that’s because it is. Remember, TypeScript is JavaScript, with types and a few other features added on. So this makes it very easy to work with if you already understand JavaScript, but it is not difficult to learn if you have other programming experience.
We’ll talk about some other things we can do with TypeScript as we get more complicated and start to Learn TypeScript.
Installing TypeScript was originally found on Access 2 Learn