Optional properties in Interface
In TypeScript interfaces optional properties can be defined by using the ‘?’ modifier after the property name. This indicates that a property may or may not be present on objects of that interface type.
Example: In the above example,the properties name and age are required but the property designation is optional.
interface Employee {
name: string;
designation?: string; // Optional property
age: number;
}
const employee1: Employee = {
name: "John",
age: 30,
};
const employee2: Employee = {
name: "Jessica",
designation:"Developer",
age: 25,
};
console.log(employee1);
console.log(employee2);
Output:
TypeScript Optional Properties Type
TypeScript Opional properties type provides a way of defining the parts that are not necessarily required.
Contact Us