Open In App

Typescript Partial<Type> Utility Type

Last Updated : 12 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Partial<Type> utility in TypeScript creates a new type by making all properties of an existing type optional. This allows you to create flexible object types where only some properties are required, streamlining code and reducing redundancy when working with complex data structures.

What is Partial<Type>?

Partial<Type> is a TypeScript utility type that transforms all properties of a given type T into optional properties. This simplifies creating objects with optional properties, saving time and reducing the likelihood of introducing errors.

Syntax:

type Partial<T> = {
[P in keyof T]?: T[P];
};

Where:

  • T: T is the type we want to make partial.
  • keyof: This operator is used to a union of all the property names in T.
  • ? operator: It is used set the property to optional.

Approach: If we already have a typescript interface with some properties, and all of the properties are initially required. Now let’s say, we have a scenario where we need to use the already existing interface but we don’t need all its properties of it has, so instead of making a new interface with some of its properties, we can use the same interface and create a new type with all the properties of it optional by using the Partial<Type>, so in that case we can use any properties to do the job. 

Example 1: Using the Partial<Type> Utility in TypeScript

// Define an interface for a Person
interface Person {
    name: string;
    age: number;
    address: string;
}

// Use Partial to make all Person properties optional
type PartialPerson = Partial<Person>;

// Create a PartialPerson with only the name property
const person: PartialPerson = { name: 'John' };

// Log the person object
console.log(person);

Explanation:

  • Original Interface: The User interface has required properties: name, age, and email.
  • Partial Transformation: Partial<User> creates a new type with all properties optional.
  • Object Creation: The updateUser object only defines name, making age and email optional.

Output:

{ name: 'John' }

Example 2: Using Partial<Type> in a Function

// Define a type for a User object
type User = {
    name: string;
    age: number;
};

// Define a function to return a string from Partial<User>
function getUser(user: Partial<User>): string {
    // Create a string with name and age properties
    return `Name: ${user.name}, Age: ${user.age || 'Unknown'}`;
}

// Create user objects
const user1 = { name: 'John', age: 30 };
const user2 = { name: 'Mary' };

// Call the getUser function
console.log(getUser(user1));
console.log(getUser(user2));

Explanation:

  • Original Type: The User type has mandatory name and age properties.
  • Function Definition: getUser accepts a Partial<User> object, making name and age optional.
  • Function Output: When called, getUser handles both complete and incomplete User objects, defaulting age to “Unknown” if missing.

Output:

Name: John, Age: 30
Name: Mary, Age: Unknown

Conclusion

In this article, we explored the Partial<Type> utility type in TypeScript, its syntax, and practical examples demonstrating how to use it. Partial<Type> is a useful tool for creating new types with only some of the properties of an existing type, especially helpful when working with complex objects or functions that require many properties to be defined. By using Partial<Type>, developers can write cleaner, more maintainable code and avoid redundant type definitions.



Next Article

Similar Reads