jeudi 6 mai 2021

Check if Property in Typescript Object is Optional

I'm trying to create dynamic form validation based on the interface of a model.

My test model interface is as so:

IRole {
   id: string;
   name: string;
   description?: string;
}

I have two instances of Role

Without description:

const role1: IRole = {
   grants: ['admin:*'];
   name: 'Admin'; 
}

With description:

const role2: IRole = {
   grants: ['owner:*'];
   name: 'User';
   desciption: 'Average Joe';
}

Required fields for a form would be:

[grants, name]

Option field would be:

[description]

The interface has the details I need. This doesn't seem possible to do but wanted to check with others before I abandon this approach.





Aucun commentaire:

Enregistrer un commentaire