Code Reference

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

IdeaMeaning
Type checkerCatches many bugs before running code
tscOfficial TypeScript compiler CLI
tsconfig.jsonProject compiler options
EmitOutput .js (and optional .d.ts declarations)
Gradual typingYou 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 --version

Tiny file (hello.ts):

const message: string = "Hello, TypeScript";
console.log(message);

Compile and run:

npx tsc hello.ts
node hello.js

Minimal tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "strict": true,
    "outDir": "dist",
    "rootDir": "src"
  },
  "include": ["src"]
}

⚠️ Pitfalls

  • Running .ts directly needs tsx, ts-node, or a bundler — Node does not execute TS natively (without experimental flags).
  • Types do not exist at runtime: typeof still sees JS values only.
  • Skipping "strict": true hides the value of TypeScript.
  • Editing emitted .js by hand will be overwritten on next compile.

On this page