Sunday, December 2, 2018

optional properties at an interface in TypeScript

Use a question mark immediately after the name like so:

interface Cat {
   Name: string;
   Lives: number;
   Birthday?: Date;
}
let cat: Cat = <Cat>{
   Name: "Frank",
   Lives: 9
}
alert(cat.Name);

 
 

You make an "optional" property at a class in the same way.

No comments:

Post a Comment