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." category: "Programming Languages" category_slug: programming-languages tags: "typescript,types,static-typing,generics,interfaces" view_count: 0 created_at: 1777673262 updated_at: 1777673262 data: concepts - 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; } const 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; } let 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'; } if (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; } type 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 }; function 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]; } type 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; type 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."