Optional properties in Type
Type aliases allow you to define optional properties in a similar way to interfaces, using the ‘?’ modifier.
Example: In this example,the properties userId and name are required but the property email is optional.
type User = {
userId: number;
name:string;
email?: String; // Optional property
};
const user1: User = {
userId: 1001,
name:"Anne",
email: "anne@email.com",
};
const user2: User = {
userId: 1002,
name:"Smith",
};
console.log(user1);
console.log(user2);
Output:
TypeScript Optional Properties Type
TypeScript Opional properties type provides a way of defining the parts that are not necessarily required.
Contact Us