type a = 'one' | 'two'
I would like to have a type b like
type b = 'ONE' | 'TWO'
So I tried
type a = 'one' | 'two'
type b = {[P in a]: P['toUpperCase']}
But that does not do what I want it to do.
Thank you for reading :)
You can now do this with the introduction of Template Literal Types:
type A = 'one' | 'two'
type B = Uppercase<A>
let b: B = 'one' // Type '"one"' is not assignable to type '"ONE" | "TWO"'.
In addition to Uppercase<StringType>, there are also the following helper types:
They can be used within a Template Literal Types, like below:
type Fruit = 'Apple' | 'Banana'
type FruitField = `fr_${Uncapitalize<Fruit>}`
const fruit: Record<FruitField, boolean> = {
'fr_apple': true,
'fr_banana': false,
'fr_Apple': true, // error
'fr_peach': false // error
}
{uppercase A}. typescriptlang.org/play?ts=4.1.0-beta#code/…Here you have the link to TS Playground
And here the code:
type A = 'one' | 'two'
type B = Uppercase<A>
let b: B = 'one' // Error
P.S. I don't get why in the previous comment has been used a more complicated form to write the same thing
EDIT: Derek Nguyen (best answer) has now changed his answer with the newer and more simple solution.
type a = 'one' | 'two'on a separate file and the read the file content, then programmaticly change the values to uppercase.lowercase.d.tsand generates anuppercase.d.tswith all the string literal types in uppercase there. But I'm not sure I really like this. Seems really easy to get in a tight bind with this if you need, say, first later capitalised only or anything other that deviates.