How do you define a paradigm type to return all attribute names that implement a specific type in the original type?

type KeyOfByType<T extends object, P> = ???

type T1 = {
    a: string
    b: number
    c: string
    d: object
}
type T2 = KeyOfByType<T1, string>
// type T2 = "a" | "c"

how do you define KeyOfByType above?

Jul.11,2022

type NoneStringKeys<T, K> = {
  [P in keyof T]: T[P] extends K ? never : P;
}[keyof T];

type KeyOfByType<T, K> = keyof Pick<T, Exclude<keyof T, NoneStringKeys<T, K>>>;

interface T1 {
    a: string
    b: number
    c: string
    d: object
}

type T2 = KeyOfByType<T1, string>;
Menu