api: YAML JSON TOON Database
version: 1.0.0
format: json
dataset:
  id: 21
  slug: typescript-type-system
  title: TypeScript Type System
  description: "TypeScript's static type system: types, interfaces, generics, utility types, and advanced type manipulation."
  summary: The TypeScript type system's main concepts, from primitive types, interfaces and generics to union and intersection types, type guards, utility types, discriminated unions, mapped and conditional types and template literal types, each with a description and example.
  use_when:
    - You need the TypeScript construct for a typing problem and an example.
    - You are reading advanced type definitions in a library.
  caveats:
    - TypeScript's types are erased at runtime; they do not validate data coming from outside.
    - Complex conditional and mapped types can slow the compiler and are hard to read; use them with care.
  related:
    - javascript-es6-features
    - csharp-dotnet-features
    - language-comparison-2026
    - solid-principles
  category: Programming Languages
  category_slug: programming-languages
  tags: typescript,types,static-typing,generics,interfaces
  view_count: 0
  created_at: 1777673262
  updated_at: 1777673262
  entry_count: 12
  fields:
    - section: concepts
      field: name
      type: string
      example: Primitive Types
    - section: concepts
      field: types
      type: array
      example: "[7 items]"
    - section: concepts
      field: description
      type: string
      example: Basic built-in types. TypeScript adds `any`, `u…
    - section: concepts
      field: example
      type: string
      example: "let name: string = \"Alice\"; let age: number = 3…"
    - section: concepts
      field: keyword
      type: string
      example: interface
    - section: concepts
      field: patterns
      type: array
      example: "[4 items]"
    - section: concepts
      field: common
      type: array
      example: "[6 items]"
    - section: concepts
      field: pattern
      type: string
      example: tagged union
data:
  concepts:
    - name: Primitive Types
      types:
        - string
        - number
        - boolean
        - "null"
        - undefined
        - symbol
        - bigint
      description: Basic built-in types. TypeScript adds `any`, `unknown`, `never`, `void`.
      example: "let name: string = \"Alice\"; let age: number = 30; let isActive: boolean = true;"
    - name: Interfaces
      keyword: interface
      description: Define object shape; can be extended and implemented. Structural typing.
      example: "interface User { id: number; name: string; email?: string; }\nconst u: User = { id: 1, name: \"Bob\" };"
    - name: Type Aliases
      keyword: type
      description: Create new named type; can represent primitives, unions, tuples, objects.
      example: "type ID = number | string; type Point = [number, number]; type User = { name: string };"
    - name: Generics
      keyword: <T>
      description: Type variables enable reusable components that work with multiple types.
      example: "function identity<T>(arg: T): T { return arg; }\nlet output = identity<string>(\"hello\");"
    - name: Union Types
      keyword: "|"
      description: Value can be one of several types; use type guards to narrow.
      example: "function format(x: string | number) { if (typeof x === 'string') { ... } else { ... } }"
    - name: Intersection Types
      keyword: "&"
      description: Combine multiple types into one; object must satisfy all constraints.
      example: "type Admin = User & { permissions: string[] };"
    - name: Type Guards
      patterns:
        - typeof
        - instanceof
        - in operator
        - custom type predicate
      description: Narrow types within conditional blocks.
      example: "function isString(x: unknown): x is string { return typeof x === 'string'; }\nif (isString(val)) { val.toUpperCase(); }"
    - name: Utility Types
      common:
        - Partial<T>
        - Required<T>
        - Pick<T,K>
        - Omit<T,K>
        - Record<K,T>
        - ReturnType<T>
      description: Built-in type transformations; avoid manual re-declaration.
      example: "interface Post { title: string; content: string; published: boolean; }\ntype EditPost = Pick<Post, 'title' | 'content'>;"
    - name: Discriminated Unions
      pattern: tagged union
      description: Union of types with common literal field; enables exhaustive type checking in switch.
      example: "type Shape = { kind: 'circle'; radius: number } | { kind: 'square'; side: number };\nfunction area(s: Shape) { switch(s.kind) { case 'circle': return Math.PI * s.radius ** 2; ... } }"
    - name: Mapped Types
      keyword: keyof, in
      description: Create types by mapping over properties of another type.
      example: "type Readonly<T> = { readonly [P in keyof T]: T[P]; }\ntype Optional<T> = { [P in keyof T]?: T[P] };"
    - name: Conditional Types
      keyword: "T extends U ? X : Y"
      description: Types that choose between two types based on a condition.
      example: "type IsString<T> = T extends string ? true : false;\ntype NonNullable<T> = T extends null | undefined ? never : T;"
    - name: Template Literal Types
      keyword: "`${}`"
      description: Build strings from other types; useful for CSS property names, API endpoints.
      example: type EventName = `on${Capitalize<string>}`; // onLoad, onMouseEnter, etc.
