Documentation
    Preparing search index...

    Type Alias ExtractField<T, K>

    ExtractField: T extends Record<K, infer U> ? U : never

    ExtractField is a utility type that extracts the type of a specific field from a type T. This type takes two parameters:

    • T, the type from which the field type is to be extracted.
    • K, the key of the field to be extracted from T.

    The utility type works as follows:

    • T extends Record<K, infer U>: This conditional check verifies if T can be assigned to a record type with a key K and some value. If it can, TypeScript infers the type of this value and assigns it to U.

    • U is returned when T is assignable to Record<K, infer U>, i.e., when T has the key K.

    • If T does not have the key K, the utility type returns never.

    This utility type is powerful as it allows the extraction of a specific field's type dynamically, given the field's name. It is useful when working with complex and dynamic types where the structure might not be known ahead of time.

    Type Parameters

    • T
    • K extends keyof any