TypeScript Type System

TypeScript's static type system: types, interfaces, generics, utility types, and advanced type manipulation.

  • JSON7,800 B
  • YAML5,458 B · −30%
  • TOON4,902 B · −37%

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 it when

  • You need the TypeScript construct for a typing problem and an example.
  • You are reading advanced type definitions in a library.

Watch out for

  • 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.

The data

Concepts

NameTypesDescriptionExampleKeywordPatternsCommonPattern
Primitive Types
  • string
  • number
  • boolean
  • null
  • undefined
  • symbol
  • bigint
Basic built-in types. TypeScript adds `any`, `unknown`, `never`, `void`.let name: string = "Alice"; let age: number = 30; let isActive: boolean = true;nullnullnullnull
InterfacesnullDefine object shape; can be extended and implemented. Structural typing.interface User { id: number; name: string; email?: string; } const u: User = { id: 1, name: "Bob" };interfacenullnullnull
Type AliasesnullCreate new named type; can represent primitives, unions, tuples, objects.type ID = number | string; type Point = [number, number]; type User = { name: string };typenullnullnull
GenericsnullType variables enable reusable components that work with multiple types.function identity<T>(arg: T): T { return arg; } let output = identity<string>("hello");<T>nullnullnull
Union TypesnullValue can be one of several types; use type guards to narrow.function format(x: string | number) { if (typeof x === 'string') { ... } else { ... } }|nullnullnull
Intersection TypesnullCombine multiple types into one; object must satisfy all constraints.type Admin = User & { permissions: string[] };&nullnullnull
Type GuardsnullNarrow types within conditional blocks.function isString(x: unknown): x is string { return typeof x === 'string'; } if (isString(val)) { val.toUpperCase(); }null
  • typeof
  • instanceof
  • in operator
  • custom type predicate
nullnull
Utility TypesnullBuilt-in type transformations; avoid manual re-declaration.interface Post { title: string; content: string; published: boolean; } type EditPost = Pick<Post, 'title' | 'content'>;nullnull
  • Partial<T>
  • Required<T>
  • Pick<T,K>
  • Omit<T,K>
  • Record<K,T>
  • ReturnType<T>
null
Discriminated UnionsnullUnion of types with common literal field; enables exhaustive type checking in switch.type Shape = { kind: 'circle'; radius: number } | { kind: 'square'; side: number }; function area(s: Shape) { switch(s.kind) { case 'circle': return Math.PI * s.radius ** 2; ... } }nullnullnulltagged union
Mapped TypesnullCreate types by mapping over properties of another type.type Readonly<T> = { readonly [P in keyof T]: T[P]; } type Optional<T> = { [P in keyof T]?: T[P] };keyof, innullnullnull
Conditional TypesnullTypes that choose between two types based on a condition.type IsString<T> = T extends string ? true : false; type NonNullable<T> = T extends null | undefined ? never : T;T extends U ? X : Ynullnullnull
Template Literal TypesnullBuild strings from other types; useful for CSS property names, API endpoints.type EventName = `on${Capitalize<string>}`; // onLoad, onMouseEnter, etc.`${}`nullnullnull

Fields

The shape of this dataset, read from the data itself: every column, its type and a real value. Agents can use this to plan a query without fetching the whole document.

SectionFieldTypeExample
conceptsnamestringPrimitive Types
conceptstypesarray[7 items]
conceptsdescriptionstringBasic built-in types. TypeScript adds `any`, `u…
conceptsexamplestringlet name: string = "Alice"; let age: number = 3…
conceptskeywordstringinterface
conceptspatternsarray[4 items]
conceptscommonarray[6 items]
conceptspatternstringtagged union

Fetch the same bytes

The static files contain the same reference rows as the API, with no API rate limit. Live metadata, such as view counts, may differ. Use the API for content negotiation; use the static files for a cacheable snapshot.

curl "https://yjtoon.yunusemrevurgun.com/api/dataset/typescript-type-system?format=toon"
const res = await fetch(
  "https://yjtoon.yunusemrevurgun.com/static-data/dataset/typescript-type-system.toon"
);
const toon = await res.text();

Rate limit: 120 requests per minute per IP, no key and no signup. API reference →

Topics

  • typescript
  • types
  • static-typing
  • generics
  • interfaces

Search the library

Search by a topic, title, or keyword.

Start with a collection

Browse all 205 datasets