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

  • Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Detecting Duplicates With Machine Learning in 2026: From String Metrics to Embeddings and LLMs
  • Java to TypeScript in a Day: AI Migration Playbook
  • When TypeScript Types Meet Untrusted AI: Building Type-Safe LLM Pipelines With Runtime Validation

Trending

  • Reusable Operational Playbooks With Agent Skills
  • What 55 Runs Taught Me About Memory in AI Coding Agents
  • Migrating From the OpenAI API to vLLM: Configure It Right
  • How Has AI Impacted Code Obfuscation?

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. 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
·
Jul. 05, 22 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
5.6K 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

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
  • Detecting Duplicates With Machine Learning in 2026: From String Metrics to Embeddings and LLMs
  • Java to TypeScript in a Day: AI Migration Playbook
  • When TypeScript Types Meet Untrusted AI: Building Type-Safe LLM Pipelines With Runtime Validation

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