Sam Denty
Sam Denty

Reputation: 4085

Check if an interface has a required field

Is it possible to check whether an interface has a required field using Typescript's Conditional Types?

type AllRequired = { a: string; b: string }
type PartiallyRequired = { a: string; b?: string }
type Optional = { a?: string; b?: string }

// Is it possible to change this, so the below works
type HasRequiredField<T> = T extends {} ? true : false

type A = HasRequiredField<AllRequired> // true
type B = HasRequiredField<PartiallyRequired> // true
type C = HasRequiredField<Optional> // false

Upvotes: 4

Views: 1846

Answers (2)

AllAwesome497
AllAwesome497

Reputation: 330

Found another, simpler way to do this that works in at least TS 3.8.3.

// if Partial<T> extends T none of the fields are required.
type HasRequiredFields<T> = Partial<T> extends T ? false : true

Example use cases

type Info<TInfo> = Partial<TInfo> extends TInfo
   ? { info?: TInfo }
   : { info: TInfo };

type A = { type: 'A' } & Info<{ abc?: string }>
type B = { type: 'B' } & Info<{ abc: string }>

// Valid
let a1: A = { type: 'A' };
// Valid
let a2: A = { type: 'A', info: {} };
// Valid
let a3: A = { type: 'A', info: { abc: 'hello' } };
// Invalid
let a4: A = { type: 'A', info: { abc: 3 } };

// Invalid
let b1: B = { type: 'B' }
// Invalid
let b2: B = { type: 'B', info: {} };
// Valid
let b3: B = { type: 'B', info: { abc: 'Hello' } };
// Invalid
let b4: B = { type: 'B', info: { abc: 5 } };

Upvotes: 1

jcalz
jcalz

Reputation: 328132

Yes, you can detect if properties are optional. It gets a bit iffy with index signatures, but your types don't have them so I'm not going to worry about them.

Here's how you can extract just the keys of the non-optional properties of an object type

type RequiredKeys<T> = { [K in keyof T]-?:
  ({} extends { [P in K]: T[K] } ? never : K)
}[keyof T]

And then you can just check if it has any of them or not (if RequiredKeys<T> is never then it does not):

type HasRequiredField<T> = RequiredKeys<T> extends never ? false : true

And that gives your desired results:

type A = HasRequiredField<AllRequired> // true
type B = HasRequiredField<PartiallyRequired> // true
type C = HasRequiredField<Optional> // false

Hope that helps; good luck!

Upvotes: 5

Related Questions