EdwardZZZ / articles

工作点滴记录
2 stars 0 forks source link

TS #86

Open EdwardZZZ opened 3 weeks ago

EdwardZZZ commented 3 weeks ago
type FlatKeys<T> = T extends object
    ? { [K in keyof T] : T[K] extends Function ? never : K | FlatKeys<T[K]> }[keyof T]
    : never;

class Test {
    a: number;
    b: string;
    c: C;

    getField(field: FlatKeys<this>) {}
}

class C {
    d: boolean;
    e: string;
}

class T extends Test {
    f: number;
}

const t = new T();
t.getField('a');
EdwardZZZ commented 3 weeks ago
type NestedObject = {
  prop1: {
    nestedProp1: string;
    nestedProp2: number;
  };
  prop2: string;
};

type Subtract<A extends number, B extends number> = 
  [...Array<A>, ...Array<B>] extends [...Array<infer L>]
  ? L
  : never;

type ExtractKeys<T, Depth extends number = 10> = [Depth] extends [never]
  ? never
  : T extends object
  ? keyof T | {
      [K in keyof T]: T[K] extends infer U
        ? U extends object
          ? ExtractKeys<U, Subtract<Depth, 1>>
          : never
        : never;
    }[keyof T]
  : never;

type AllKeys = ExtractKeys<NestedObject>;