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
Newsletter
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
  • Two AI Agents, One Database: Solving Concurrent Updates With Optimistic Locking
  • A Non-Expiring AKS Deployment Token Could Read Secrets Across Three Namespaces

Trending

  • Every Generation Fears the Machine. Here Is How the AI Job Panic Usually Ends
  • Avoiding AI Debt as a Team
  • How to Build a Scalable SaaS Product Architecture
  • Claude Code Superpowers: The 5-Tool Stack That Ends Limits and Context Amnesia

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Apr. 17, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
4.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • Two AI Agents, One Database: Solving Concurrent Updates With Optimistic Locking
  • A Non-Expiring AKS Deployment Token Could Read Secrets Across Three Namespaces

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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