Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: an interface property requires another property to be true

Tags:

How can I define keys: a, b, c, bar as undefined/null/optional type if foo is false? In other words, I need these properties to be mandatory only if foo is true.

interface ObjectType {
  foo: boolean;
  a: number;
  y: string;
  c: boolean;
  bar?: { x: number; y: string; z: boolean };
}

Thanks! :)

like image 621
Vinay Sharma Avatar asked Nov 20 '20 17:11

Vinay Sharma


1 Answers

I think the most straight forward way is to simply use union types.

interface RequiredObjectType {
  foo: true;
  a: number;
  y: string;
  c: boolean;
  bar: { x: number; y: string; z: boolean };
}

interface OptionalObjectType {
  foo: false;
  a?: number;
  y?: string;
  c?: boolean;
  bar?: { x: number; y: string; z: boolean };
}

type AnyObjectType = RequiredObjectType| OptionalObjectType;

You could of course abstract the repeated properties out if needed to save typing on types that will change overtime.

interface ObjectTypeValues {
  a: number;
  y: string;
  c: boolean;
  bar: { x: number; y: string; z: boolean };
}

interface RequiredObjectType extends ObjectTypeValues {
  foo: true
}

interface OptionalObjectType extends Partial<ObjectTypeValues> {
  foo: false
}

type AnyObjectType = RequiredObjectType | OptionalObjectType;

You'll get type inference for free as well.

if (type.foo) {
  // im the required type!
  // type.a would be boolean.
} else {
  // im the optional type.
  // type.a would be boolean?
}
like image 195
Brenden Avatar answered Oct 12 '22 23:10

Brenden