Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
Define a TypeScript type that takes a lowercase word

Is there any way to define that kind of type in TypeScript? Something like:

type lowercaseWord = /[a-z]/

But that defines a string instead? (I think the code above will define a regex.)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Yes and no. There is built int (intrinsic) type LowerCase but it works as an utility type. It works as String.prototype.toLowerCase and not as a regex. See example:

type Result = Lowercase<'A'> // 'a'

Playground

Here you can find more types: Capitalize, Uncapitalize, Lowercase, Uppercase

If you want to apply such restriction it is possible to do in the function:

type UpLetters = 'A' | 'B' | 'C' // provide all allowed letters
type LowLetters = Lowercase<UpLetters>

type Letters = UpLetters | LowLetters

type IsAllowed<T extends string> = T extends Letters ? T extends Lowercase<T> ? T : never : never

const lower = <T extends string>(str: IsAllowed<T>) => null

/**
 * Ok
 */
lower('a') // ok

/**
 * Error
 */
lower('A') // error
lower('1') // error
lower('$%^') // error

Please keep in mind that

const x: Lowercase<string> = 'A' // no error

will not trigger an error

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!