// Type aliases create a name for any type expression
Note Type aliases and interfaces overlap for object shapes, but type aliases can also name unions, tuples, primitives, and function types. Unlike interfaces, type aliases cannot be re-declared for merging. Use interfaces for object shapes that might be extended; use type aliases for everything else.
typeStatus="pending"|"active"|"archived";typeApiResponse=SuccessResult|ErrorResult;interfaceSuccessResult{
ok:true;
data: unknown;}interfaceErrorResult{
ok:false;
errorCode: number;
message: string;}functionhandleResponse(res:ApiResponse){if(res.ok){console.log(res.data);// narrowed to SuccessResult}else{console.error(res.message);// narrowed to ErrorResult}}
Output
// Union means 'one of these types' - must narrow before accessing specific members
Note You can only access properties common to ALL members of a union without narrowing first. Use a shared discriminant property (like 'ok' above) to let TypeScript narrow automatically in conditionals.
// Intersection combines all properties from all types
Note Intersection means 'all of these combined'. If two types have the same property with incompatible types, the intersected property becomes never (which usually means a mistake). Intersections are great for mixin-style composition.
intersection typecombine typesand typemerge typesmixin type
typeDirection="north"|"south"|"east"|"west";typeHttpMethod="GET"|"POST"|"PUT"|"DELETE"|"PATCH";typeDiceRoll=1|2|3|4|5|6;functionmove(direction:Direction, steps: number){console.log(`Moving ${direction} by ${steps}`);}move("north",3);// OK// move("up", 3); // Error: Argument of type '"up"' is not assignable
Output
// Literal types restrict a value to an exact set of allowed values
Note Literal types turn strings, numbers, or booleans into specific-value types. Combined with unions, they create powerful enumerations without the overhead of enum. const assertions (as const) automatically infer literal types.
literal typestring literalexact value typespecific valuesenum alternative
Note Template literal types construct new string types from combinations. They distribute over unions - every combination is produced. Built-in string manipulation types (Capitalize, Uppercase, Lowercase, Uncapitalize) work inside template literals. Extremely powerful for typing DSLs and API patterns.
template literal typestring pattern typedynamic string typestring manipulation type
// Generic aliases accept type parameters just like functions accept value parameters
Note Generic type aliases are essential for building reusable type utilities. Default type parameters (B = A) make common cases convenient while keeping the alias flexible. You can also add constraints with extends.
generic type aliasparameterized typereusable typetype with generic