Home > Blockchain >  How do I tell Typecript an interface will not have any undefined members
How do I tell Typecript an interface will not have any undefined members

Time:06-18

I have an interface like this

interface Person {
 name?:string;
age? :number;
gender?:string
}

I would like to use that exact same interface type somewhere else. But, I want to modify it telling that all members will never be undefined. So, it will be like this

 name:string;
age :number;
gender:string

Is there a way to modify an interface without rewriting an entire interface? Thanks in advance

CodePudding user response:

Simply use the Required<Type> utility type

Constructs a type consisting of all properties of Type set to required. The opposite of Partial.

type RequiredPerson = Required<Person>;

Note: Requires at least TypeScript v2.8

  • Related