Home > database >  Typescript create uber-strict types
Typescript create uber-strict types

Time:07-11

If I create a custom type such as:

type LowercaseString = string

And then use it in a function like this:

function displayLowercaseString(input: LowercaseString) {
  ...
}

displayLowercaseString('UPPERCASE')

The above example compiles perfectly and runs, since 'UPPERCASE' is of type string and so is LowercaseString

My question is, is it possible to have a TS error such as type string does not match LowercaseString so that I am forced to put every string through a function like this before using it in displayLowercaseString:

function stringToLowercase(input: string): LowercaseString {
  return input.toLowercase as LowercaseString
}

CodePudding user response:

You can use a branded type :

type LowercaseString = string & { __brand: 'lower' };


function stringToLowercase(input: string): LowercaseString {
  return input.toLowerCase() as LowercaseString
}

function displayLowercaseString(input: LowercaseString) {
}

const uppercaseString = 'UPPERCASE';
displayLowercaseString(uppercaseString) // nope 
displayLowercaseString(stringToLowercase(uppercaseString)) // ok

Playground

  • Related