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 Extract Type Works

How the TypeScript Extract Type Works

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

Johnny Simpson user avatar by
Johnny Simpson
CORE ·
Apr. 14, 22 · Web Dev Zone · Tutorial
Like (3)
Save
Tweet
2.67K Views

Join the DZone community and get the full member experience.

Join For Free

The Extract utility type lets us check a union type for specific members, and returns a new type based on what is leftover. It's quite similar in format to the Exclude type.

Let's find out how it works.

Utility Types

Utility Types are types defined in TypeScript to solve particular problems. If you're new to defining custom types in TypeScript, read my guide on defining custom types here.

How the Extract Type Works in Typescript

In TypeScript, we can define a specific type called a union type. A union type is a list of possible values for something. An example is shown below, where the type myUnionType means variables and other outputs can only be one of four values: x, y, z, or a.

TypeScript
 
type myUnionType = "x" | "y" | "z" | "a"

// This works since x is a member of "x" | "y" | "z" | "a"
let myFirstVariable:myUnionType = "x"

// This doesn't work since "my-string" is NOT member of "x" | "y" | "z" | "a"
let mySecondVariable:myUnionType = "my-string"

The Extract Type

If we want to remove specific elements from a union type, we can use the Exclude Type - but there are other ways we can manipulate union types.

The Extract Type lets us define a new list, and returns a new type if any items in that list exist in our original type.

Let's look at a quick example:

TypeScript
 
type myUnionType = "x" | "y" | "z" | "a"

let myFirstVariable:Extract<myUnionType, "x" | "y"> = "x"
//  ^
//  └ - - Type is "x" | "y"

When we write Extract, Extract checks myUnionType for "x" | "y". If they exist, it makes a new type containing the items that exist. Since both x and y exist in our union type, we end up with a new type - "x" | "y".

If we define members in our Extract statement that don't exist in our original union type, then they will be ignored in the new type. For example:

TypeScript
 
type myUnionType = "x" | "y" | "z" | "a"

let myFirstVariable:Extract<myUnionType, "x" | "y" | "b"> = "x"
//  ^
//  └ - - Type is STILL "x" | "y" since "b" is not in myUnionType

Using Extract does not affect the original type, so we can still use it if we want:

TypeScript
 
type myUnionType = "x" | "y" | "z" | "a"

let myFirstVariable:Extract<myUnionType, "x" | "y" | "b"> = "x"
//  ^
//  └ - - Type is "x" | "y"

let mySecondVariable:myUnionType = "z"
//  ^
//  └ - - Type is "x" | "y" | "z" | "a"

Extract, therefore, is a great tool when we want to limit our original union type to a set number of defined members for specific variables or outputs. It gives us flexibility in letting us define types on the fly, which we can use anywhere in our code.

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

  • Top 20 Git Commands With Examples
  • Is NoOps the End of DevOps?
  • MEAN vs MERN Stack: Which One Is Better?
  • Component Testing of Frontends: Karate Netty Project

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