TypeScript Type System

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

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

Fetch the same bytes

The static files are identical to what the API returns, but with no rate limit and no server round trip. Use the API when you want a query and a content type; use the files when you want to cache one document.

curl "https://yjtoon.com/api/dataset/typescript-type-system?format=toon"
const res = await fetch(
  "https://yjtoon.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