0

I'm trying to make a generic type which will make keys of the given type required if they are a certain type. Rest of the keys should be optional or omitted.

Consider the following example:

interface Foo {
    prop1: string;
    prop2: boolean;
    prop3: boolean;
    prop4: number;
}

/*
* Make all the keys with boolean type required
* Rest of the keys can be optional or omitted
*/
const foo: BooleanOnlyFields<Foo> = {
    prop2: true,
    // Should show error that prop3 is required because it's boolean type
}

Is it possible to implement something like BooleanOnlyFields in Typescript?

AC101
  • 851
  • 8
  • 13
  • 1
    See the other question/answer for how to do this. Translating the code from that answer to your example yields [this](https://tsplay.dev/nmqOjw). – jcalz Feb 18 '21 at 20:49
  • Thanks. This is exactly what I was looking for. – AC101 Feb 18 '21 at 20:51

0 Answers0