DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • TypeScript: Useful Features
  • Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Token Attribution Framework for Agentic AI in CI/CD
  • Why Round-Robin Won't Save You: Load Balancing Challenges in Data Streaming Services With Heterogeneous Traffic

Trending

  • Implementing Observability in Distributed Systems Using OpenTelemetry
  • Prompt Injection Is Real, So I Built a Python Firewall for LLM Pipelines
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Building a Spring AI Assistant With MCP Servers: A Step-by-Step Tutorial
  1. DZone
  2. Data Engineering
  3. Data
  4. How the TypeScript Partial Type Works

How the TypeScript Partial Type Works

Let's take a look at how the Partial utility type works in TypeScript.

By 
Johnny Simpson user avatar
Johnny Simpson
·
Apr. 17, 22 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
3.9K Views

Join the DZone community and get the full member experience.

Join For Free

The Partial type in TypeScript is a utility type that does the opposite of Required. It sets all properties in a type to optional.

Let's look at how it works. If you're interested in the opposite problem, try my article on how the Required type works in TypeScript.

Partial Type in TypeScript

In TypeScript, we can define a type as optional with a ? question mark. For example, in the below code, lastName is optional. Therefore, even though firstUser has the type User, we can leave out lastName as it is not required.

TypeScript
 
type User = {
    firstName: string,
    lastName?: string
}

let firstUser:User = {
    firstName: "John"
}

Sometimes, the type we inherit or use has no optional types, but we know that in certain circumstances some properties might be missing. Take a look at the following example. Here, lastName is not optional anymore, but firstUser still doesn't have it:

TypeScript
 
type User = {
    firstName: string,
    lastName: string
}

let firstUser:User = {
    firstName: "John"
}

This code throws an error since it is expecting that firstUser of type, the User should have a property called lastName:

Plain Text
 
Property `lastName` is missing in type `{ firstName: string; }` but required in type `User`.

If we want to avoid that error, we have to change firstUser's type to Partial<User>. That will tell TypeScript to set every property in the type User to optional:

TypeScript
 
type User = {
    firstName: string,
    lastName: string
}

let firstUser:Partial<User> = {
    firstName: "John"
}

This is ultimately the same as redefining the User type to:

TypeScript
 
type User = {
    firstName?: string,
    lastName?: string
}

The only difference is, that we can now use both - if we want the type to have some missing properties, we can use Partial. If we don't, we can just use the normal User type:

TypeScript
 
type User = { 
    firstName: string, 
    lastName: string 
}
let firstUser:Partial<User> = { firstName: "John" } 
let secondUser:User = { firstName: "John", lastName: "Doe" }

As with many other utility types in TypeScript, Partial is meant to work with interfaces or custom types defined as objects like our User type above. So it won't work on things like variables.

If you've enjoyed this quick guide, check out more TypeScript content here.

TypeScript Data Types

Published at DZone with permission of Johnny Simpson. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • TypeScript: Useful Features
  • Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Token Attribution Framework for Agentic AI in CI/CD
  • Why Round-Robin Won't Save You: Load Balancing Challenges in Data Streaming Services With Heterogeneous Traffic

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook