| |

TypeScript 1 ๐Ÿ”ท Introduction to TypeScript

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. Every valid JavaScript program is already a valid TypeScript program โ€” TypeScript just adds a layer of static types on top, checks them at build time, and strips them away before the code ever runs in a browser or Node. It was created by Microsoft in 2012 and has since become the default language for large-scale JavaScript projects. Angular, NestJS, and much of the modern tooling ecosystem are written in it.

Key point: TypeScript doesn’t run in the browser. It’s a compile-time tool. You write .ts files, the TypeScript compiler checks them and emits .js files, and those .js files are what actually execute. The types exist only during development โ€” they leave zero footprint in the final bundle.


What TypeScript is

TypeScript is JavaScript plus a type system. That’s the whole idea.

function greet(name: string): string {
  return `Hello, ${name}!`;
}

greet('Alice');   // โœ…
greet(42);        // โŒ compile error

That : string annotation is TypeScript. The function body is plain JavaScript. When you compile, the annotation disappears and you’re left with:

function greet(name) {
  return `Hello, ${name}!`;
}

The type was checked at build time โ€” before the code ever ran. If you’d passed a number, you’d have known immediately, not after a user hit a bug in production.

What TypeScript adds:

  • Static types โ€” variables, parameters, and return values can be annotated
  • Type inference โ€” you don’t have to annotate everything; TypeScript figures out most types
  • Structural typing โ€” types are compared by shape, not by name
  • Interfaces and generics โ€” tools for describing complex data and reusable abstractions
  • Modern JavaScript features โ€” TypeScript compiles newer syntax down for older runtimes
  • Editor support โ€” autocomplete, refactoring, and inline errors via the language server

What TypeScript does not add:

  • Runtime type checking โ€” types don’t exist at runtime
  • A different runtime โ€” it runs wherever JavaScript runs
  • Performance improvements โ€” the emitted JavaScript is essentially what you’d write by hand

Why a superset matters: You can rename a .js file to .ts and it will compile โ€” that’s the promise of “superset.” You don’t rewrite anything. You add types incrementally, file by file, until the whole project is typed. That’s what makes TypeScript adoptable in existing codebases.


Why TypeScript exists

JavaScript was designed in ten days in 1995 for small scripts. It has grown into the language of the web, servers, mobile, and desktop โ€” and its dynamic type system, which is flexible, is also the source of an enormous class of bugs. undefined is not a function, [object Object], silent coercion โ€” these are the daily hazards of untyped JavaScript.

TypeScript’s answer: catch type errors before they run.

// JavaScript
function calculateTotal(items) {
  return items.reduce((sum, item) => sum + item.price, 0);
}
// TypeScript
interface Item {
  name: string;
  price: number;
}

function calculateTotal(items: Item[]): number {
  return items.reduce((sum, item) => sum + item.price, 0);
}

In the TypeScript version, you can’t accidentally pass a string array, call calculateTotal() with no argument, or access a property that doesn’t exist on Item. The compiler rejects the code before it’s ever executed.

Where this pays off:

  • Refactoring โ€” rename a field and every use is updated or flagged
  • Team code โ€” new developers see the shape of data without reading the whole codebase
  • Large projects โ€” the compiler catches bugs that would only surface at runtime in JS
  • Tooling โ€” editors offer autocomplete, go-to-definition, and inline errors because they know the types
  • Documentation โ€” types are executable documentation; they can’t drift from reality

The cost: an extra build step and a bit more syntax. In exchange, you get a compiler that finds entire categories of bugs for you.

Why “catch errors before they run” is the real pitch: Every benefit of TypeScript โ€” better tooling, safer refactoring, clearer APIs โ€” flows from that one property. If a bug can be caught at compile time, it never reaches the user. That’s the promise of a statically typed language, and it’s what JavaScript has always lacked.


TypeScript is a compile-time tool

The compiler is called tsc. It reads .ts files and produces .js files.

hello.ts  โ”€โ”€โ–บ  tsc  โ”€โ”€โ–บ  hello.js

The compiler does three things:

  1. Type-checks โ€” reports errors for type mismatches
  2. Transforms โ€” rewrites newer syntax for older targets (e.g., optional chaining for ES5)
  3. Emits โ€” writes .js (and optionally .d.ts declaration files)

The type annotations don’t appear in the output. They’re erased.

Two implications:

Types don’t affect runtime behavior. A string and a number are the same at runtime โ€” the distinction only exists in the type checker. instanceof checks on TypeScript types won’t work because the types don’t exist.

Runtime data is untyped. If you read JSON from an API, TypeScript has no idea what shape it is. JSON.parse() returns any. You either validate the data or trust your type annotation โ€” the compiler can’t verify either way.

interface User {
  name: string;
}

const user = JSON.parse('{"name": 42}') as User;
console.log(user.name.toUpperCase());  // โœ… compiles, โŒ crashes at runtime

The compiler trusts your as User assertion. It can’t verify that the JSON actually matched. Types are contracts you write, not guarantees the runtime enforces.

Why this matters: TypeScript catches type errors in your code, but it can’t validate data from outside your program. That’s why runtime validation libraries โ€” Zod, io-ts, Valibot โ€” exist. They check the shape of incoming data and produce a typed result the compiler can then trust.


Superset, not replacement

TypeScript isn’t a new language. It’s JavaScript with optional type syntax.

const x = 5;              // โœ… valid TS and JS
const y: number = 5;      // โœ… valid TS, erases to `const y = 5`

Every JavaScript feature โ€” classes, promises, closures, generators, modules, destructuring โ€” works in TypeScript exactly as it does in JavaScript. The type system is layered on top; it doesn’t replace anything.

What that means in practice:

  • You can start with one .ts file in a .js project
  • You can turn off strictness and compile with almost no changes
  • You can add types gradually, one file at a time
  • You can drop types entirely by not annotating and letting inference do the work

What it doesn’t mean:

  • You don’t get runtime type safety for free
  • You still have to think about JavaScript’s dynamic behavior
  • You can’t “compile away” all bugs โ€” just a specific class of them

Why gradual adoption was a design goal: Microsoft knew developers wouldn’t rewrite their projects. By making TypeScript a superset with optional types and a compile-to-JS model, they made adoption incremental. That’s the main reason the language spread so fast.


The type system in one example

TypeScript’s type system is structural โ€” types are compared by shape, not by name.

interface Point {
  x: number;
  y: number;
}

function distanceFromOrigin(p: Point): number {
  return Math.sqrt(p.x ** 2 + p.y ** 2);
}

// This object isn't "declared as" a Point โ€” it just has the same shape
const p = { x: 3, y: 4 };
distanceFromOrigin(p);   // โœ…

p was never declared as a Point. It just has the properties Point requires, so it satisfies the type. That’s structural typing โ€” also called “duck typing” at the type level.

Two directions this goes:

Compatible shapes pass. Any object with x: number and y: number works, regardless of what else it contains.

Missing or wrong-shaped members fail. An object with x: string or without y won’t compile.

distanceFromOrigin({ x: '3', y: 4 });   // โŒ x is string, not number
distanceFromOrigin({ x: 3 });            // โŒ missing y

Why structural: It matches JavaScript’s object model. JS doesn’t have nominal types โ€” it has shapes. TypeScript follows the language it’s modeling. That means you can pass a { x, y, color } object where { x, y } is expected โ€” extra properties are fine, as long as the required ones are present.

Why this is powerful: Structural typing makes interoperability easy. Two libraries that both define a Point interface don’t need to share the type โ€” as long as their shapes match, the values are interchangeable. That’s how TypeScript works with dozens of competing libraries without central type definitions for everything.


A first TypeScript program

Write a file called hello.ts:

interface User {
  id: number;
  name: string;
}

function greet(user: User): string {
  return `Hello, ${user.name} (id ${user.id})!`;
}

const alice: User = { id: 1, name: 'Alice' };

console.log(greet(alice));
console.log(greet({ id: 2, name: 'Bob' }));

Compile:

npx tsc hello.ts

Produces hello.js:

"use strict";
function greet(user) {
  return `Hello, ${user.name} (id ${user.id})!`;
}
const alice = { id: 1, name: 'Alice' };
console.log(greet(alice));
console.log(greet({ id: 2, name: 'Bob' }));

The interface and the type annotations are gone. The logic is preserved. Run it with Node:

node hello.js

Try introducing an error:

console.log(greet({ id: 'one', name: 'Carol' }));  // โŒ id is string

The compiler rejects it before any file is emitted.

Why this example: It’s the smallest program that shows the full loop โ€” write typed code, compile, run. Everything else in the language extends this pattern: annotate, compile, execute the emitted JavaScript.


When to use TypeScript

Good fit:

  • Large codebases โ€” more code means more places a type error can hide
  • Team projects โ€” shared types reduce miscommunication
  • Long-lived applications โ€” refactoring is far safer with the compiler on your side
  • Public libraries โ€” typed APIs are documentation that can’t lie
  • Apps with complex data โ€” modeling domain rules in types catches real bugs
  • Angular, NestJS, and modern React/Vue projects โ€” the ecosystems are built around TypeScript

Less obvious fit:

  • Tiny scripts โ€” a 20-line file may not benefit enough to justify setup
  • Rapid prototypes โ€” types can slow exploration; add them after the shape stabilizes
  • One-off CLI tools โ€” often not worth the extra build step
  • Learning JavaScript โ€” you should understand JS first; TypeScript’s types make more sense once the underlying language does

The honest answer: TypeScript is worth it for most non-trivial projects. The cost is small; the payoff compounds as the project grows.


TypeScript vs JavaScript โ€” a quick comparison

AspectJavaScriptTypeScript
TypesDynamic, runtimeStatic, compile-time
Runs directlyโœ…โŒ (must compile)
Type errors caughtAt runtimeAt build time
ToolingBasicRich (autocomplete, refactor)
Learning curveLowerHigher
Best forSmall scripts, prototypesLarge apps, teams, libraries
EcosystemEverythingEverything + typed libraries

JavaScript is not “worse.” It’s a different trade-off. TypeScript adds a compiler and a type system in exchange for catching bugs earlier.

Why both exist and matter: JavaScript is the runtime. TypeScript is a tool for writing it more safely. The relationship isn’t competition โ€” TypeScript emits JavaScript. Every TS project is also a JS project in the end.


A full example

A small program that models a shopping cart.

interface Product {
  id: number;
  name: string;
  price: number;
}

interface CartItem {
  product: Product;
  quantity: number;
}

function cartTotal(items: CartItem[]): number {
  return items.reduce((sum, item) => sum + item.product.price * item.quantity, 0);
}

function formatCurrency(amount: number): string {
  return `$${amount.toFixed(2)}`;
}

const cart: CartItem[] = [
  { product: { id: 1, name: 'Book', price: 12.5 }, quantity: 2 },
  { product: { id: 2, name: 'Pen', price: 1.75 }, quantity: 3 }
];

console.log(`Total: ${formatCurrency(cartTotal(cart))}`);
// โ†’ Total: $30.25

Every piece of data is described. Product, CartItem, the array type, the return types. The compiler verifies that item.product.price is a number, that cart matches CartItem[], and that formatCurrency receives a number.

If someone renames price to cost, every use fails to compile โ€” a rename you’d otherwise chase across the codebase by hand.

Why this shape: TypeScript’s value shows up the moment data gets non-trivial. Two interfaces and three functions, and you already have type-safe arithmetic on a collection of structured objects. That’s the everyday case โ€” not fancy generics, just structured data with a compiler watching.


Complete Example Session

# ============================================
# PART 1: CHECK NODE IS INSTALLED
# ============================================

node --version
# [ v20.11.0 ]

# ============================================
# PART 2: INSTALL TYPESCRIPT LOCALLY
# ============================================

npm install --save-dev typescript
# [ added 1 package ]

# ============================================
# PART 3: VERIFY THE COMPILER
# ============================================

npx tsc --version
# [ Version 5.4.0 ]

# ============================================
# PART 4: WRITE A FIRST PROGRAM
# ============================================

cat > hello.ts << 'EOF'
interface User {
  id: number;
  name: string;
}

function greet(user: User): string {
  return `Hello, ${user.name} (id ${user.id})!`;
}

const alice: User = { id: 1, name: 'Alice' };
console.log(greet(alice));
EOF

# ============================================
# PART 5: COMPILE
# ============================================

npx tsc hello.ts
# (no output โ€” success)

ls
# [ hello.js  hello.ts ]

# ============================================
# PART 6: INSPECT THE EMITTED JAVASCRIPT
# ============================================

cat hello.js
# [ "use strict"; ]
# [ function greet(user) { ]
# [   return `Hello, ${user.name} (id ${user.id})!`; ]
# [ } ]
# [ const alice = { id: 1, name: 'Alice' }; ]
# [ console.log(greet(alice)); ]
# (types are gone)

# ============================================
# PART 7: RUN IT
# ============================================

node hello.js
# [ Hello, Alice (id 1)! ]

# ============================================
# PART 8: TRIGGER A TYPE ERROR
# ============================================

cat > broken.ts << 'EOF'
function greet(name: string): string {
  return `Hello, ${name}!`;
}

greet(42);
EOF

npx tsc broken.ts
# [ broken.ts:5:7 - error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. ]
# [ ]
# [ 5 greet(42); ]
# [         ~~ ]

# (no JS was emitted for this file)

# ============================================
# PART 9: CREATE A TSCONFIG
# ============================================

npx tsc --init
# [ Created a new tsconfig.json with: ]
# [   target: es2016 ]
# [   module: commonjs ]
# [   strict: true ]
# [   ... ]

# ============================================
# PART 10: COMPILE EVERYTHING IN THE PROJECT
# ============================================

npx tsc
# (compiles every .ts in the project per tsconfig.json)

Quick Reference

What TypeScript Is

AspectValue
Full nameTypeScript
TypeSuperset of JavaScript
AddsStatic types, tooling
Compiles toPlain JavaScript
Created byMicrosoft
First release2012
RuntimeNone โ€” it’s a compile-time tool

Key Concepts

ConceptMeaning
Static typingTypes checked at build time
InferenceTypeScript figures out types automatically
Structural typingTypes compared by shape
Type erasureTypes removed during compilation
SupersetEvery JS file is valid TS
Gradual adoptionAdd types file by file

Common Commands

CommandPurpose
npm install --save-dev typescriptInstall locally
npm install -g typescriptInstall globally
npx tsc file.tsCompile a single file
npx tscCompile per tsconfig.json
npx tsc --initCreate tsconfig.json
npx tsc --versionShow compiler version
npx tsc --watchWatch and rebuild
npx tsc --noEmitType-check only, don’t emit

What Compiles and What Doesn’t

CodeCompiles?
const x = 5;โœ…
const x: number = 5;โœ…
const x: number = 'a';โŒ
function f(n: number) {}โœ…
f('a');โŒ
JSON.parse('{}') as Userโœ… (trusts you)
window as stringโŒ

TypeScript vs JavaScript

AspectJavaScriptTypeScript
TypesRuntime, dynamicCompile-time, static
Build stepNoneRequired
ToolingBasicRich
Catches type errorsAt runtimeAt build time
Learning curveLowerHigher
Best forSmall scriptsLarge apps, teams

Compiler Output

InputOutput
.ts.js
.ts + --declaration.js + .d.ts
.tsx.js
Type annotationsErased
InterfacesErased
EnumsEmitted as JS objects

Adoption Checklist

StepPurpose
Install TypeScriptGet the compiler
Create tsconfig.jsonConfigure the project
Rename one .js to .tsStart small
Set strict: false initiallyAvoid overwhelming errors
Add types incrementallyFile by file
Turn on strict checksOnce codebase is typed

Best Practices

โœ… Do This:

// Let inference do its job
const name = 'Alice';           // โœ… inferred as string

// Annotate function parameters and returns
function greet(name: string): string { ... }  // โœ…

// Use interfaces for object shapes
interface User { id: number; name: string; }  // โœ…

// Validate runtime data
const user = UserSchema.parse(json);          // โœ…

// Enable strict mode
// In tsconfig.json: "strict": true           // โœ…

// Use the compiler in CI
npx tsc --noEmit                              // โœ…

โŒ Don’t Do This:

// Don't over-annotate obvious types
const n: number = 5;                          // โš ๏ธ  inference is enough

// Don't use `any` to silence errors
function f(x: any) { ... }                    // โŒ defeats the purpose

// Don't trust `as` on external data
const u = JSON.parse(str) as User;            // โš ๏ธ  runtime unchecked

// Don't skip the build step in production
node app.ts                                   // โŒ Node runs JS, not TS

// Don't turn off strict mode forever
// "strict": false                            // โš ๏ธ  temporary at best

// Don't expect runtime type checks
if (typeof x === 'User') { }                  // โŒ User doesn't exist at runtime

Common Pitfalls

PitfallProblemSolution
Expecting runtime typesTypes are erasedValidate external data
Using any everywhereNo type safetyUse unknown then narrow
Over-annotatingNoiseLet inference work
Forgetting to compileNode won’t run .tsRun tsc or use a runner
Mixing up as and runtime checksas is a claim, not a checkValidate with Zod or manual
Skipping strict modeWeak checkingEnable strict: true
Not committing tsconfig.jsonInconsistent buildsCommit it
Confusing interfaces and valuesThey exist in different worldsInterfaces for types, objects for values

Real-World Examples

1. A typed function

function add(a: number, b: number): number {
  return a + b;
}

2. A typed object

interface Point { x: number; y: number; }
const p: Point = { x: 1, y: 2 };

3. Inferred types

const name = 'Alice';  // TypeScript knows this is string

4. Union type

let id: string | number;
id = 'abc';
id = 42;

5. Type error caught early

add('a', 'b');  // โŒ compile error

6. Array of typed objects

interface Item { name: string; price: number; }
const items: Item[] = [{ name: 'Book', price: 12.5 }];

7. Optional property

interface User { id: number; nickname?: string; }

8. Type alias

type ID = string | number;

9. Readonly

interface Config { readonly apiUrl: string; }

10. Enum

enum Status { Loading, Ready, Error }
const s: Status = Status.Loading;

11. Generic function

function identity<T>(x: T): T { return x; }

12. Narrowing

function f(x: string | number) {
  if (typeof x === 'string') return x.toUpperCase();
  return x.toFixed(2);
}

13. Compile and run

npx tsc hello.ts && node hello.js

14. Watch mode

npx tsc --watch

15. Type-check only

npx tsc --noEmit

Visual: Compile Pipeline

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  hello.ts                                    โ”‚
โ”‚                                              โ”‚
โ”‚  interface User { ... }                      โ”‚
โ”‚  function greet(u: User): string { ... }     โ”‚
โ”‚  const a: User = { ... };                    โ”‚
โ”‚                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                  โ”‚
                  โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  tsc                                         โ”‚
โ”‚                                              โ”‚
โ”‚  โ€ข Type-check                                โ”‚
โ”‚  โ€ข Transform syntax                          โ”‚
โ”‚  โ€ข Erase types                               โ”‚
โ”‚                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                  โ”‚
                  โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  hello.js                                    โ”‚
โ”‚                                              โ”‚
โ”‚  function greet(u) { ... }                   โ”‚
โ”‚  const a = { ... };                          โ”‚
โ”‚                                              โ”‚
โ”‚  (no types โ€” plain JavaScript)               โ”‚
โ”‚                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                  โ”‚
                  โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  node hello.js                               โ”‚
โ”‚                                              โ”‚
โ”‚  โ†’ Runs in the JS runtime                    โ”‚
โ”‚                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Visual: Where Errors Are Caught

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  JavaScript                                  โ”‚
โ”‚                                              โ”‚
โ”‚  Write code โ”€โ”€โ–บ run โ”€โ”€โ–บ crash (maybe)        โ”‚
โ”‚                                              โ”‚
โ”‚  Errors caught at runtime โ€” in production    โ”‚
โ”‚                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  TypeScript                                  โ”‚
โ”‚                                              โ”‚
โ”‚  Write code โ”€โ”€โ–บ COMPILE โ”€โ”€โ–บ run              โ”‚
โ”‚                    โ”‚                         โ”‚
โ”‚                    โ–ผ                         โ”‚
โ”‚              Type errors here                โ”‚
โ”‚              (before shipping)               โ”‚
โ”‚                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Visual: Superset Relationship

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  TypeScript                                  โ”‚
โ”‚                                              โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚  JavaScript                            โ”‚ โ”‚
โ”‚  โ”‚                                        โ”‚ โ”‚
โ”‚  โ”‚  โ€ข variables, functions, classes       โ”‚ โ”‚
โ”‚  โ”‚  โ€ข promises, closures, modules         โ”‚ โ”‚
โ”‚  โ”‚                                        โ”‚ โ”‚
โ”‚  โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚ โ”‚
โ”‚  โ”‚  โ”‚  + Type annotations              โ”‚  โ”‚ โ”‚
โ”‚  โ”‚  โ”‚  + Interfaces, generics          โ”‚  โ”‚ โ”‚
โ”‚  โ”‚  โ”‚  + Type-level tooling            โ”‚  โ”‚ โ”‚
โ”‚  โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚ โ”‚
โ”‚  โ”‚                                        โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚                                              โ”‚
โ”‚  Every JS program is also a TS program      โ”‚
โ”‚                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Visual: What’s Erased

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  In TypeScript source                        โ”‚
โ”‚                                              โ”‚
โ”‚  interface User { id: number; name: string; }โ”‚
โ”‚  function greet(u: User): string { ... }     โ”‚
โ”‚  const x: number = 5;                        โ”‚
โ”‚                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                  โ”‚
                  โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  After compilation                           โ”‚
โ”‚                                              โ”‚
โ”‚  function greet(u) { ... }                   โ”‚
โ”‚  const x = 5;                                โ”‚
โ”‚                                              โ”‚
โ”‚  (interface, types โ€” all gone)               โ”‚
โ”‚                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Visual: The Type System’s Reach

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Your code                                   โ”‚
โ”‚                                              โ”‚
โ”‚  โœ… TypeScript checks here                   โ”‚
โ”‚                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  External data (API, JSON, user input)       โ”‚
โ”‚                                              โ”‚
โ”‚  โŒ TypeScript CANNOT check here             โ”‚
โ”‚                                              โ”‚
โ”‚  Use runtime validation (Zod, io-ts, etc.)   โ”‚
โ”‚                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Runtime                                     โ”‚
โ”‚                                              โ”‚
โ”‚  โŒ No types exist here                      โ”‚
โ”‚                                              โ”‚
โ”‚  Errors are plain JavaScript errors          โ”‚
โ”‚                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

ConceptMeaning
TypeScriptTyped superset of JavaScript
SupersetEvery JS program is valid TS
Compile-timeTypes checked at build, erased at runtime
Static typingTypes known before execution
Structural typingTypes compared by shape
InferenceCompiler figures out types
Type erasureTypes vanish in emitted JS
tscThe TypeScript compiler
tsconfig.jsonProject configuration
Strict modeStricter type checks โ€” recommended

Key takeaways:

  • TypeScript is a typed superset of JavaScript โ€” every JS file is already valid TS
  • It’s a compile-time tool โ€” .ts files compile to .js, and types are erased in the process
  • Types are checked before runtime, catching entire categories of bugs early
  • The type system is structural โ€” types match by shape, not by name
  • Inference means you don’t annotate everything โ€” TypeScript figures out most types
  • Type erasure means types don’t exist at runtime โ€” they can’t be checked against live data
  • External data (APIs, JSON, user input) must be validated at runtime โ€” TypeScript can’t help there
  • Adoption is gradual โ€” rename one file, add types incrementally, turn on strictness over time
  • tsconfig.json configures the compiler; strict: true is the recommended baseline
  • tsc --noEmit type-checks without emitting โ€” useful in CI

Remember: TypeScript is JavaScript with a type layer on top. The types exist at compile time, catch bugs before they run, and disappear before the code executes. It doesn’t replace JavaScript โ€” it makes it safer to write at scale. The runtime is still JavaScript, the semantics are still JavaScript, and the bugs TypeScript doesn’t catch โ€” external data, runtime behavior โ€” are still your responsibility. That’s the trade: a small build step in exchange for a compiler that finds your mistakes before your users do.


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!