Getting Started with TypeScript
TypeScript · Reference cheat sheet
Getting Started with TypeScript
TypeScript · Reference cheat sheet
📋 Overview
TypeScript (TS) is JavaScript plus a static type system. You write .ts / .tsx files; the compiler (tsc) checks types and emits plain JavaScript. Types exist at compile time — they are erased at runtime.
🔧 Core concepts
| Idea | Meaning |
|---|---|
| Type checker | Catches many bugs before running code |
tsc | Official TypeScript compiler CLI |
tsconfig.json | Project compiler options |
| Emit | Output .js (and optional .d.ts declarations) |
| Gradual typing | You can add types step by step |
Prereqs: Node.js installed. Then npm install -D typescript in a project (or use npx tsc).
💡 Examples
Install and check version:
npm install -D typescript
npx tsc --versionTiny file (hello.ts):
const message: string = "Hello, TypeScript";
console.log(message);Compile and run:
npx tsc hello.ts
node hello.jsMinimal tsconfig.json:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"]
}⚠️ Pitfalls
- Running
.tsdirectly needstsx,ts-node, or a bundler — Node does not execute TS natively (without experimental flags). - Types do not exist at runtime:
typeofstill sees JS values only. - Skipping
"strict": truehides the value of TypeScript. - Editing emitted
.jsby hand will be overwritten on next compile.