TypeScript basics: what the language is and where it is used
What TypeScript is
TypeScript is JavaScript with a type checker in front of it.
Every valid JavaScript program is already a TypeScript program, and a
type annotation only says what a value is meant to be: the compiler
reads the annotations and refuses to produce JavaScript when they
disagree.
The annotations disappear at compile time. What reaches the browser or
Node is ordinary JavaScript with no runtime
library and no new syntax, which is why TypeScript can be added to an
existing project one file at a time. Types also document the code: an
editor shows the shape of a value before the program is started.
Where it runs
The compiler emits plain JavaScript, so a TypeScript program runs
wherever JavaScript runs: browsers, Node.js,
Deno and Bun. The build step is the only difference from a JavaScript
project, and the bundler of the project usually takes it over:
Vite compiles the ts files as it
serves them.
The language started at Microsoft in 2012 and now ships the type
definitions of most of the JavaScript ecosystem. Angular is written in
it, and Vue, React and Express projects commonly use it without giving
up their JavaScript packages.
Install the compiler
The compiler is one npm package, and Node brings npm with it. Install it once for the whole machine:
npm i -g typescript
tsc --version
Version 5.4.5
A newer release prints its own number, and any recent version compiles the code on this page.
Hello, TypeScript
Save these two lines as main.ts. The annotation after the colon is the only TypeScript part: it promises that message holds text.
const message: string = "Hello, TypeScript";
console.log(message);
Compile the file by name. Silence means success: tsc writes main.js next to main.ts and prints nothing.
tsc main.ts
The emitted file is JavaScript, so node runs it and the annotation is already gone:
node main.js
Hello, TypeScript
Break the promise on purpose and the compiler stops the build instead of the program:
const message: string = 42;
main.ts(1,7): error TS2322: Type 'number' is not assignable to type 'string'.
The course
The TypeScript course takes the same file through a hundred lessons: basic and advanced types, classes, generics and the compiler first, then Node, asynchronous code, tests, the DOM and three capstone projects. Every lesson ends with a link to the next one.
The version numbers and the output on this page were taken from the TypeScript course, which used Node 20.11.0, npm 10.2.4 and TypeScript 5.4.5 on x86-64. A newer release prints its own numbers.
Article author: Arthur Isaev