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[2]: You need the TypeScript construct for a typing problem and an example.,You are reading advanced type definitions in a library. caveats[2]: 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[4]: 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[8]{section,field,type,example}: concepts,name,string,Primitive Types concepts,types,array,"[7 items]" concepts,description,string,"Basic built-in types. TypeScript adds `any`, `u…" concepts,example,string,"let name: string = \"Alice\"; let age: number = 3…" concepts,keyword,string,interface concepts,patterns,array,"[4 items]" concepts,common,array,"[6 items]" concepts,pattern,string,tagged union data: concepts[12]: - name: Primitive Types types[7]: 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: description: Type variables enable reusable components that work with multiple types. example: "function identity(arg: T): T { return arg; }\nlet output = identity(\"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[4]: 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[6]: Partial,Required,"Pick","Omit","Record",ReturnType description: Built-in type transformations; avoid manual re-declaration. example: "interface Post { title: string; content: string; published: boolean; }\ntype EditPost = Pick;" - 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 = { readonly [P in keyof T]: T[P]; }\ntype Optional = { [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 extends string ? true : false;\ntype NonNullable = 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}`; // onLoad, onMouseEnter, etc."