my-swiss-knife: - v1.9.9
    Preparing search index...

    Type Alias StringNumberKeys<T>

    StringNumberKeys: Extract<
        { [K in keyof T]: T[K] extends string | number ? K : never }[keyof T],
        keyof T,
    >

    Extracts the keys of an object type whose values are of type string or number.

    This utility type iterates over the keys of a given type T and returns a union of those keys whose corresponding values extend either string or number.

    It is useful when you want to constrain operations (e.g., indexing or mapping) only to fields that are compatible with being used as object keys (e.g., for hashmaps).

    Type Parameters

    • T

      The object type to extract keys from.

    type Example = {
    id: number;
    name: string;
    isActive: boolean;
    };

    type Result = StringNumberKeys<Example>; // "id" | "name"
    export type StringNumberKeys<T> = Extract<{
    [K in keyof T]: T[K] extends string | number ? K : never
    }[keyof T], keyof T>;