Open In App

How to Use Partial Types in TypeScript ?

Last Updated : 04 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Partial types in TypeScript offer a versatile solution for creating new types with a subset of properties from existing types. This feature proves invaluable in scenarios where you need to work with incomplete data structures or define flexible interfaces.

Below are the approaches to using partial types in TypeScript:

Using the Partial<T> Utility Type

The Partial<T> utility type in TypeScript enables developers to create new types where all properties of the original type T are optional. This approach simplifies the process of defining partial types and is particularly useful for complex data structures.

Syntax:

Partial<T>

Example: The below code explains the use of the Partial<T> utility type with interfaces.

Javascript




interface User {
    name: string;
    est: number;
    desc: string;
}
 
type PartialUser = Partial<User>;
const partialUser: PartialUser = {
    name: "GeeksforGeeks",
    est: 2009
};
console.log("PartialUser type:", partialUser);


Output:

PartialUser type:  { name: "GeeksforGeeks", est: 2009}

Creating functions with Partial Type Parameters

In TypeScript, you can use partial types to define functions with optional or partial type parameters. This approach allows you to create functions that accept objects with only a subset of properties, providing flexibility and ensuring type safety.

Syntax:

type PartialParams = Partial<{ prop1: type1; prop2: type2; }>;
function partialFunction(params: PartialParams) {
// Function implementation
}

Example: The below code explains the way of creating functions that accept the parameters of partial type.

Javascript




type PartialParams =
    Partial<{ prop1: string; prop2: number; }>;
 
function partialFunction(params: PartialParams) {
    console.log("Received params:", params);
}
 
partialFunction({ prop1: "Hello" });
partialFunction({ prop2: 42 });
partialFunction({ prop1: "Hello", prop2: 42 });


Output:

Received params: { prop1: 'Hello' }
Received params: { prop2: 42 }
Received params: { prop1: 'Hello', prop2: 42 }

Using Partial Types with Mapped Types

Mapped types in TypeScript allow you to create new types by transforming each property in an existing type. Partial types can be effectively used with mapped types to create new types with optional or modified properties based on the original type.

Syntax

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

Example: The below code will use the partial types to create mapped types.

Javascript




type PartialType<T> =
    { [P in keyof T]?: T[P] };
 
interface User {
    name: string;
    age: number;
    email: string;
}
 
type PartialUser = PartialType<User>;
 
const partialUser: PartialUser =
    { name: 'GeeksforGeeks' };
console.log("Partial User:", partialUser);


Output:

Partial User: { name: 'GeeksforGeeks' }


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads