- Explain what static types are and which bugs they catch in advance
- Describe how TypeScript code turns into JavaScript
- Set up a project with
tscand a basictsconfig.json - Run
.tsfiles directly with Node.js ortsxand check the types withtsc --noEmit
Imagine an online shop where a function formatPrice(price) has worked perfectly for months. One day the price arrives from a form as the string '4.5' instead of a number, and the page breaks with TypeError: price.toFixed is not a function. The customer saw the bug before the developer did. TypeScript exists so that you, not your users, find such bugs — right in your editor, the moment you type them.
What is TypeScript?
A programming language created by Microsoft that adds static types to JavaScript. Every valid JavaScript program is also a TypeScript program; TypeScript simply lets you describe what kinds of values variables, parameters and functions hold.
In JavaScript, types are checked dynamically — only when a line actually runs. In TypeScript they are checked statically — by reading the code, without running it. You write the type after a colon; this is called a type annotation: price: number means “price is always a number”. If you pass a string anywhere, the compiler tells you at once:
function formatPrice(price: number): string {
return price.toFixed(2) + ' AZN';
}
console.log(formatPrice(4.5));
console.log(formatPrice('4.5'));: string says that the function returns a string. The second call is a mistake.shop.ts:6:25 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
6 console.log(formatPrice('4.5'));
~~~~~shop.ts:6:25), the error code TS2345 and the reason. The wavy line marks the exact spot.From TypeScript to JavaScript
Browsers and Node.js really understand only JavaScript. That is why the TypeScript compiler, tsc, does two jobs: it checks the types, and it emits ordinary JavaScript in which all the types are simply erased. Here is a small file and the JavaScript that tsc produces from it:
type User = { name: string; age: number };
export function greet(user: User): string {
return `Hello, ${user.name}! You are ${user.age}.`;
}
const aysel: User = { name: 'Aysel', age: 14 };
console.log(greet(aysel));Hello, Aysel! You are 14.
greet.ts: type User describes the shape of the object.export function greet(user) {
return `Hello, ${user.name}! You are ${user.age}.`;
}
const aysel = { name: 'Aysel', age: 14 };
console.log(greet(aysel));tsc, greet.js: the type User line and all annotations are gone, while the logic is exactly the same.What types give you
- Bugs are caught early: a typo in a property name, a wrong argument or a forgotten
nullcheck is found before the program runs. - A smart editor: because the editor knows what every value is, autocompletion, go-to-definition and safe renaming across the whole project just work.
- Types work as documentation: the header
formatPrice(price: number): stringtells you how to use the function without reading its body. - Safe changes in big projects: change a function's parameters and the compiler lists every place that has to be updated.
Setting up a project
- 1Install TypeScript
In the project folder, run
npm install --save-dev typescript. The compiler goes intonode_modules, and you call it withnpx tsc. - 2Create the config file
npx tsc --initcreates atsconfig.jsonfile with recommended settings. - 3Write the code
Put your code in
src/index.ts. - 4Compile and run
npx tscchecks the whole project and writes the JavaScript todist/; then run it withnode dist/index.js.
{
"compilerOptions": {
"target": "es2022",
"module": "nodenext",
"strict": true,
"rootDir": "src",
"outDir": "dist"
}
}tsconfig.json. tsc --init generates more options, but these are the essentials.| Option | What it does |
|---|---|
target | chooses which JavaScript version to emit, for example es2022 |
module | chooses the module system; nodenext follows the rules of modern Node.js |
strict | turns on the full set of strict checks — always keep it true |
rootDir / outDir | where the .ts sources live and where the .js output goes |
Running .ts files directly
Modern Node.js (version 22.18 and newer, including Node.js 24) can run a .ts file directly: node index.ts. It uses type stripping: Node.js simply cuts the types out and runs the JavaScript that remains. The tsx tool does the same job in older versions of Node.js too.
node src/index.ts # Node.js 22.18+: strip the types and run
npx tsx src/index.ts # tsx: run TypeScript on older Node.js too
npx tsc --noEmit # only check the types, write no filesKey points
- TypeScript adds static types to JavaScript; bugs are found before the code runs.
- A type annotation is written after a colon:
price: number. tscchecks the types and emits plain JavaScript with the types erased; at runtime there are no types.- Project settings live in
tsconfig.json;strict: trueshould always be on. node file.tsandtsxrun the code but don't check types — that is whattsc --noEmitis for.
Check yourself
10 questions. Every correct answer earns XP.