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

  • Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Swift: Master of Decoding Messy JSON
  • Beyond the Chatbot: Engineering a Real-World GitHub Auditor in TypeScript
  • Resilient API Consumption in Unreliable Enterprise Networks (TypeScript/React)

Trending

  • Liquid Glass, Material 3, and a Lot of Plumbing
  • Advanced Error Handling and Retry Patterns in Enterprise REST Integrations
  • Good Data, Bad Metric: A Mutation Testing Pattern for Analytics Engineering
  • Prompt Injection Is Real, So I Built a Python Firewall for LLM Pipelines
  1. DZone
  2. Coding
  3. JavaScript
  4. How Intrinsic Type Manipulations Work in TypeScript

How Intrinsic Type Manipulations Work in TypeScript

TypeScript provides intrinsic type manipulations to let us change our strings into different formats, like uppercase, lowercase, or capitalized versions of themselves.

By 
Johnny Simpson user avatar
Johnny Simpson
·
Jul. 05, 22 · Code Snippet
Likes (2)
Comment
Save
Tweet
Share
5.4K Views

Join the DZone community and get the full member experience.

Join For Free

Did you know that TypeScript comes with a bunch of built-in string manipulator types? That means we can easily transform a string type into uppercase, lowercase, or capitalized versions of itself. When might this be useful? In lots of situations where you have a fixed set of string values, for example, whenever you have a set of strings from an object using something like keyof.

To transform a string into a different version by defining a new type along with one of the type manipulators. For example, below, we transform myType into the capitalized version:

 
type myType = "hi there"
type capitalizedMyType = Capitalize<myType>

let myVar:capitalizedMyType = "Hi there";


In the above example, hi there would throw an error - only Hi there will work with a Capitalize type. Similarly, we can uncapitalize a string using Uncapitalize:

 
type myType = "HI THERE"
type uncapitalizedMyType = Uncapitalize<myType>

let myVar:uncapitalizedMyType = "hI THERE";


Above, only hI THERE will work. Any other case will fail the check.

Uppercase and Lowercase Intrinsic Type Manipulation

Along with Capitalize, we also have Uppercase and Lowercase. They both do exactly what you think they'd do - one changes all characters to uppercase, and the other to lowercase.

 
type myType = "this long sentence"
type bigMyType = Uppercase<myType>

let myVar:bigMyType = "THIS LONG SENTENCE"


Lowercase Intrinsic Type Manipulation

Above, we've created a type which is the uppercase version of myType. Here is the same example, but in the other direction: making the lowercase version:

 
type myType = "THIS LONG SENTENCE"
type smallMyType = Lowercase<myType>

let myVar:smallMyType = "this long sentence"


Using With Template Literals

These types become very useful when working with template literals, where we can enforce a string literal to use a lowercase version - for example, for an ID. Here is a version where we have a set number of strings, and we only want to use the lowercase version in the ID as part of the versionId type:

 
type versions = "DEV" | "PROD" | "STAGING"
type versionPrefix = "gold-" | "beta-" | "alpha-"
type versionId = `${versionPrefix}${Lowercase<versions>}`

let currentVersion:versionId = "gold-dev"


Above, we have two sets of union types, and we combine them into one versionId. Here, we only accept lowercase letters, but version is uppercase. We can transform it with Lowercase, so gold-dev is valid.

Using With keyof Objects

In a similar way, we may receive an object from an API or elsewhere which has an odd or uneven casing. To normalize this, we can use intrinsic type manipulations. Below, I am using keyof to get all the keys in myObject, and making them all lowercase too.

 
type myObject = {
    firstName: string,
    Age: number,
    Lastname: string
}

type keys = Lowercase<keyof myObject>

let getObjectKey:keys = "firstname"


keys only have the following valid values: firstname | age | lastname. In this example, using intrinsic string manipulation like this can be an easy way to create clean types when the data we have at our disposal has issues.

TypeScript Strings

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

Opinions expressed by DZone contributors are their own.

Related

  • Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Swift: Master of Decoding Messy JSON
  • Beyond the Chatbot: Engineering a Real-World GitHub Auditor in TypeScript
  • Resilient API Consumption in Unreliable Enterprise Networks (TypeScript/React)

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