5

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.)

alexchenco
  • 53,565
  • 76
  • 241
  • 413
  • 2
    There are no regex types in TypeScript, so there is no specific type that corresponds to "a string composed only of lowercase letters". You can make a generic type constraint and a helper function. Do you want to support or prohibit foreign character sets? [this](https://tsplay.dev/m3zKkN) accepts them and [this](https://tsplay.dev/mpDz7m) code rejects them. If you want to see either of them as an answer, let me know. – jcalz Aug 29 '21 at 02:22
  • 1
  • 1
    It's possible to do this with template literal types, but you _really don't_ want to do this. If it's not a hypothethical, perhaps consider sharing your use-case. A branded type might be the best option. – Evert Aug 30 '21 at 07:45

2 Answers2

9

(Documenting here for posterity, even though the question is slightly different and asks for [a-z])

As of TypeScript 4.8 there is now support for using the intrinsic string manipulation types with wide types like string. This was implemented in microsoft/TypeScript#47050 but not mentioned in the TS 4.8 release notes.

So now, Lowercase<string> only corresponds to lowercase strings (strings which remain unchanged after .toLowerCase()), whereas in TypeScript 4.7 and below it could match any string:

let str: Lowercase<string>;
str = "abc"; // okay
str = "DEF"; // error in TS4.8+

Playground link to code

jcalz
  • 264,269
  • 27
  • 359
  • 360
5

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