hejkerooo
hejkerooo

Reputation: 787

How to cast properties to boolean (deep)

I want to cast every type to boolean or object

type CastDeep<T, K = boolean> = {
  [P in keyof T]: K extends K[]
    ? K[]
    : T[P] extends ReadonlyArray<K>
      ? ReadonlyArray<CastDeep<K>>
      : CastDeep<T[P]>
}
interface ITest {
  city: {
    name: string,
  }
}

Expected result:

excludeProps<ITest>({
   city: true,
});

or

excludeProps<ITest>({
 city: {
  name: true
 },
});

Current error message:

  19     name: string,
           ~~~~
    The expected type comes from property 'name' which is declared here on type 'CastDeep<{ name: string; }, boolean>'

Upvotes: 4

Views: 144

Answers (1)

hejkerooo
hejkerooo

Reputation: 787

Ok, found a solution

export type ICastDeep<T> = {
  [P in keyof T]: boolean | ICastDeep<T[P]>;
}

Upvotes: 3

Related Questions