DZone
Web Dev Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > How the Typescript Required Type Works

How the Typescript Required Type Works

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

Johnny Simpson user avatar by
Johnny Simpson
CORE ·
Apr. 12, 22 · Web Dev Zone · Tutorial
Like (4)
Save
Tweet
2.27K Views

Join the DZone community and get the full member experience.

Join For Free

In TypeScript, we sometimes need to enforce that an object has required properties, even if the original type defined some of them as optional. For that, TypeScript has a utility type called Required. Let's look at how it works.

TypeScript Required Utility Type

By default, if we define a new type in TypeScript, all fields within that type are automatically required:

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

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

The above, firstUser is of type User, but it's missing lastName. As such, this code returns an error:

TypeScript
 
Property 'lastName' is missing in type '{ firstName: string; }' but required in type 'User'.

If we expect a field to be optional, we can add a question mark to the end of it in our type definition. Below, we make lastName optional by writing lastName? instead:

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

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

As such, this code does not throw an error - lastName is optional, so the fact that firstUser doesn't contain it is fine.

Forcing Optional Types To Be Required in Typescript

Sometimes, we have a situation where most of the time, lastName is optional, but in some circumstances, we require it to do something. In these cases, we can use the utility type Required. For example, if we want lastName to be required, even if it was originally defined as optional, we can write the following:

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

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

let secondUser:Required<User> = {
    firstName: "John"
}

In this example, secondUser throws an error:

TypeScript
 
Property 'lastName' is missing in type '{ firstName: string; }' but required in type 'Required<User>'

So since we've used Required, we have to add lastName to avoid the error:

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

let secondUser:Required<User> = {
    firstName: "John",
    lastName: "Doe"
}

This can give us more flexibility while allowing us to enforce field requirements for certain functionality in an application. As with other utility types, Required is meant to work with an interface or object type, like the User type we defined above. As such, it doesn't work with variables. This doesn't matter much, though, since a variable cannot have an empty value anyway.

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Writing Beautiful, Optimized, and Better .NET Code With NDepend Static Analysis
  • Spelling “Equality” With IT: Addressing the Gender Gap in the Tech Industry
  • The Best Team Ever
  • Java: Why Core-to-Core Latency Matters

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo