100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

TypeScript Advanced Types Cheat Sheet

TypeScript Advanced Types Cheat Sheet

Covers conditional types, mapped types with key remapping, template literal types, core type operators, and discriminated union narrowing.

3 PagesAdvancedMar 30, 2026

Conditional Types

Branch type-level logic on a condition, and extract types with infer.

typescript
type IsString<T> = T extends string ? true : false;type A = IsString<'hi'>; // truetype B = IsString<42>;   // false// Distributive conditional types over unionstype ToArray<T> = T extends any ? T[] : never;type C = ToArray<string | number>; // string[] | number[]// infer keyword extracts a type from within a conditionaltype ElementType<T> = T extends (infer U)[] ? U : T;type D = ElementType<number[]>; // number

Mapped Types

Transform every property of a type, including renaming keys.

typescript
type Readonly2<T> = { readonly [K in keyof T]: T[K] };type Partial2<T> = { [K in keyof T]?: T[K] };// Key remapping with 'as' (TS 4.1+)type Getters<T> = {  [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];};interface Person { name: string; age: number; }type PersonGetters = Getters<Person>;// { getName: () => string; getAge: () => number }// Filtering keys with 'never'type NonFunctionKeys<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T];

Template Literal Types

Build string literal types by composing other literal types.

typescript
type Direction = 'top' | 'bottom' | 'left' | 'right';type Margin = `margin-${Direction}`;// 'margin-top' | 'margin-bottom' | 'margin-left' | 'margin-right'type EventName<T extends string> = `on${Capitalize<T>}`;type ClickEvent = EventName<'click'>; // 'onClick'// Combined with mapped types for typed event handlerstype Handlers<T extends string> = { [K in T as EventName<K>]: () => void };

Key Type-Level Operators

Operators used to build and query types.

  • keyof- Produces a union of an object type's keys: keyof {a:1,b:2} is 'a' | 'b'
  • typeof- Extracts the static type of a value: type T = typeof someVar
  • in (mapped types)- Iterates over a union of keys to build a new object type
  • infer- Declares a type variable to be inferred inside a conditional type
  • as (key remapping)- Renames keys inside a mapped type
  • extends (constraint)- Constrains a generic parameter or drives conditional type branching
  • & (intersection)- Combines multiple types into one with all members
  • | (union)- Represents a value that could be one of several types

Discriminated Unions

Narrow a union safely using a shared literal 'kind' field.

typescript
interface Circle { kind: 'circle'; radius: number; }interface Square { kind: 'square'; side: number; }type Shape = Circle | Square;function area(shape: Shape): number {  switch (shape.kind) { // narrows the union based on the discriminant    case 'circle': return Math.PI * shape.radius ** 2;    case 'square': return shape.side ** 2;    default:      const _exhaustive: never = shape; // compile error if a case is missed      throw new Error('Unhandled shape');  }}
Pro Tip

Use the never type with a default switch case (const _exhaustive: never = value) to get a compile-time error whenever a new member is added to a discriminated union but a switch statement isn't updated to handle it.

Was this cheat sheet helpful?

Explore Topics

#TypeScriptAdvancedTypes#TypeScriptAdvancedTypesCheatSheet#Programming#Advanced#ConditionalTypes#MappedTypes#TemplateLiteralTypes#KeyTypeLevelOperators#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet