Partial Utility
In TypeScript, Partial type is a utility type that allows us to set all properties of an existing type optional. This utility is useful while working with APIs which consists of a lot of properties but only few of them are required.
Syntax:
Partial<existingType>;
Here,
- Partial : ‘Partial’ is the keyword used to set all properties of existingType as Optional.
- existingType : existingType is the name of already existing interface or type alias.
Example:
type User = {
firstName: string,
lastName: string
}
let firstUser:Partial<User> =
{ firstName: "John" }
let secondUser:User =
{ firstName: "John", lastName: "Doe" }
console.log(firstUser);
console.log(secondUser);
Output:
TypeScript Optional Properties Type
TypeScript Opional properties type provides a way of defining the parts that are not necessarily required.
Contact Us