Advanced TypeScript Type Programming: Foundations, Operators, Generics, and Real‑World Applications
This article provides a comprehensive guide to TypeScript type programming, covering basic type definitions, type operators, mapped types, conditional and infer types, generics, recursive type calculations such as Fibonacci, and practical utilities like chainable options, currying, and a compile‑time calculator.
The author, a core developer of Douyin's frontend Monorepo framework, assumes readers are already familiar with TypeScript basics and focuses on systematic type programming techniques.
1. Type Programming Basics – Types can be defined using type, interface, class, or enum. Basic types include primitives, literal types, object types, function types, tuple types, and array types.
type TypeA = '123'
interface TypeB { a: string }
type TypeC = { b: number; c: TypeA }2. Type Operators – Intersection ( &) merges object types, union ( |) creates combined types, keyof extracts keys, typeof gets the type of a value, tuple spread ( ...) concatenates tuples, and mapped types with [K in …] iterate over keys.
type A = { a: number }
type B = { b: string }
type C = A & B // { a: number; b: string }When intersecting conflicting keys, a compile‑time error occurs.
type A = { a: number }
type B = { a: string }
type C = A & B // error: Type 'number' is not assignable to type 'never' keyofexample:
interface People { a: string; b: string }
type KeyofPeople = keyof People // 'a' | 'b' typeofexample with objects and functions:
const obj = { a: '123', b: 123 }
type Obj = typeof obj // { a: string; b: number }
function fn(a: Obj, b: number) { return true }
type Fn = typeof fn // (a: Obj, b: number) => booleanEnum types require typeof before using keyof:
enum E1 { A, B, C }
type TE1 = keyof E1 // incorrect: includes prototype keys
type TE2 = keyof typeof E1 // 'A' | 'B' | 'C'3. Generics – Generic interfaces, types, and functions are declared with <T>. Constraints are added via extends, and default generic values are possible.
interface Obj1<T = string> { a: T }
type Fn2 = <T extends string | number>(...args: any[]) => anyGeneric types can be instantiated: type MyFn = Fn3<boolean>.
4. Conditional Types & Infer – The extends ? : ternary syntax enables compile‑time branching. infer extracts sub‑types within conditional types, allowing implementations of utilities like Flatten, Parameters, and ReturnType.
type Flatten<Type> = Type extends Array<infer Item> ? Item : Type
type MyParameters<T> = T extends (...args: infer P) => any ? P : neverAdvanced examples include parsing template‑literal strings and building an AST.
5. Real‑World Type Challenges – The article demonstrates a type‑level Fibonacci calculator, a chainable options pattern, a currying utility, and a compile‑time arithmetic expression evaluator.
type Fibonacci<T extends number> = T extends 1 ? 1 : T extends 2 ? 1 : Add<Fibonacci<Sub1<T>>, Fibonacci<Sub2<T>>>
type Calculator<T extends string> = Evaluate<Parse<T>>These examples showcase recursive type evaluation, conditional branching, tuple manipulation, and mapped types to achieve sophisticated type‑level computation.
Finally, the article lists common TypeScript utility types (Partial, Omit, Record, Pick, ReturnType, Parameters) and references external type‑tool libraries such as ts-toolbelt and typetype.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
TikTok Frontend Technology Team
We are the TikTok Frontend Technology Team, serving TikTok and multiple ByteDance product lines, focused on building frontend infrastructure and exploring community technologies.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
