TypeScriptTypeScript20 min read

Infer Keyword

Learn how infer extracts types inside conditional types to build advanced reusable helpers.

David Miller
December 6, 2025
1.8k79

infer lets you extract a type from another type.

    ## Example: get return type
    
    ```ts
    type MyReturn<T> = T extends (...args: any[]) => infer R ? R : never;
    
    type R1 = MyReturn<() => number>; // number
    type R2 = MyReturn<(x: string) => boolean>; // boolean
    ```
    
    ## Extract array item type
    
    ```ts
    type Item<T> = T extends (infer U)[] ? U : T;
    
    type A = Item<number[]>; // number
    type B = Item<string>;  // string
    ```
    
    ## Graph
    ```mermaid
    flowchart LR
      A[Complex Type] --> B[infer]
      B --> C[Extracted Type]
    ```
    
    ## Remember
    - infer works only in conditional types
    - Used in many built-in helpers
    
#TypeScript#Intermediate#Advanced Types