← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

TypeScript Type System

typescript 2.0 Intermediate

Also Known As

TypeScript TS types type annotations

TL;DR

TypeScript adds static types to JavaScript — catching type errors at compile time rather than runtime, while remaining a superset that compiles to plain JavaScript.

Explanation

TypeScript's type system is structural (duck typing) not nominal — if an object has the required properties, it satisfies the type regardless of its declared class. Types include primitives, unions (string|number), intersections (A&B), literal types ('GET'|'POST'), tuples, enums, and generics. The any type disables checking; unknown is the safe alternative. Type inference reduces annotation noise — TypeScript infers types from assignments. strict mode (strictNullChecks, noImplicitAny) catches the most bugs.

Common Misconception

TypeScript types exist at runtime — TypeScript is erased to JavaScript at compile time; there are no runtime type checks unless you add them explicitly (Zod, class-validator).

Why It Matters

TypeScript catches entire categories of bugs before runtime — passing the wrong argument type, accessing undefined properties, and incorrect return types are all compile-time errors, not production bugs.

Common Mistakes

  • Using any instead of unknown — any disables all type checking; unknown forces you to narrow the type before using it.
  • Not enabling strict mode — without strictNullChecks, null and undefined assignment errors are invisible.
  • Type assertions (as Type) without validation — asserting a type without checking at runtime creates false confidence.
  • Over-annotating when inference works — TypeScript infers const name = 'Alice' as string; no annotation needed.

Code Examples

✗ Vulnerable
// any disables type checking:
function process(data: any) {
    return data.user.name.toUpperCase(); // No error if data is null
}

// Type assertion without validation:
const user = response.data as User; // Unsafe if response is actually an error object
console.log(user.email.toLowerCase()); // Runtime crash if email is undefined
✓ Fixed
// unknown forces narrowing before use:
function process(data: unknown): string {
    if (typeof data !== 'object' || data === null) throw new Error('Invalid data');
    if (!('user' in data)) throw new Error('Missing user');
    const user = (data as { user: { name: string } }).user;
    return user.name.toUpperCase();
}

// Runtime validation with Zod:
const UserSchema = z.object({ email: z.string().email() });
const user = UserSchema.parse(response.data); // Throws if invalid

Added 15 Mar 2026
Edited 28 Apr 2026
Views 40
AI edit PF Media Bot Claude Opus 4.5 on refs · 27 Apr 2026
Edits history 1 edit
  1. refs PF Media Bot Claude Opus 4.5 · 27 Apr 2026
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 2 pings S 1 ping M 0 pings T 0 pings W 1 ping T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T
No pings yet today
Perplexity 10 Amazonbot 8 Unknown AI 3 Ahrefs 2 Google 2 SEMrush 2 Bing 1
crawler 26 crawler_json 1 pre-tracking 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Prefer type aliases for unions and intersections, interfaces for object shapes that will be extended — the practical difference is minimal but type aliases cannot be augmented after declaration
📦 Applies To
typescript 2.0 web cli
🔗 Prerequisites
🔍 Detection Hints
Mixing interface and type inconsistently; using any when unknown would be safer; no strict null checks causing runtime null errors
Auto-detectable: ✓ Yes typescript eslint tsc
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: File

✓ schema.org compliant