Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to conditionally require a property in a Typescript interface

Tags:

typescript

Say I have the following interface:

interface updateOptions {
  updateAll: boolean;
  fields: string[];
}

Is there a way to make updateAll required when Fields is not provided and vice versa, or is this something that must be done conditionally in the implementation?

like image 878
Oguntoye Avatar asked Sep 02 '25 10:09

Oguntoye


1 Answers

Use tagged union type:

type UpdateOptions =
  | {updateAll: true}
  | {updateAll: false, fields: string[]}

This way you can only initialize {updateAll: true} or {updateAll: false, fields: ['someField', /*...*/]}

Technically, the union doesn't need to have a tag (the boolean updateAll), but then you need to define type guard to test which type is being used in a particular branch (I assume somewhere in your code you will do if (options.updateAll) {/* ... */}):

type UpdateAllOptions = {updateAll: true}
type UpdateSomeOptions = {fields: string[]}
type UpdateOptions = UpdateAllOptions | UpdateSomeOptions

// See https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
const isAllOptions = (options: UpdateOptions): options is UpdateAllOptions =>
  (options as any).updateAll === true
like image 159
Maybe Julius Avatar answered Sep 04 '25 03:09

Maybe Julius