79324170

Date: 2025-01-02 15:15:53
Score: 1
Natty:
Report link

What about using a single required input whose model is typed in a way to make a sub-property required based on the other property's value?

Something like:

interface InputBase {
  fruitType: 'apple' | 'banana';
  fruitColor?: string; // notice that this is optionnal in the base interface
}

interface InputApple extends InputBase {
  fruitType: 'apple';
  fruitColor: string; // notice that this is now required if fruitType value is 'apple'
}

interface InputBanana extends InputBase {
}

export type InputType = InputBanana | InputApple; // this is the type you use

That way, your input would, through TypeScript validation, make sure that some property is required only if another value is x.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): What
  • Low reputation (1):
Posted by: Olivier