javascript - typescript 将字符串文字类型映射为大写

标签 javascript typescript types type-conversion typescript-typings

type a = 'one' | 'two'
我想要一个 type b喜欢
type b = 'ONE' | 'TWO'
所以我尝试了
type a = 'one' | 'two'
type b = {[P in a]: P['toUpperCase']}
但这并没有达到我想要的效果。
感谢您阅读:)

最佳答案

您现在可以在即将发布的 4.1 版本中执行此操作,并引入了 Template Literal Types。 :

type A = 'one' | 'two'
type B = `${Uppercase<A>}`

let b: B = 'one' // Type '"one"' is not assignable to type '"ONE" | "TWO"'.
TS Playground

关于javascript - typescript 将字符串文字类型映射为大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62581650/

相关文章:

javascript - 如何使用JS禁用表行并在MYSQL中插入所选选项?

javascript - 检查数组值是升序还是降序

javascript - Jquery:如何将 "selectable"值获取到 "Slider"函数中?

javascript - 通过扩展修改 Chrome Omnibox 值/文本

typescript - 如何在nestjs中动态连接数据库?

typescript - Angular 2 : Convert Observable to Promise

typescript - 当看似相同的三元运算符构造没有时,为什么使用 'if-else' 语句会产生 TypeScript 编译器错误?

typescript - 为什么我的 Typescript 对象允许这个额外的属性?

haskell - 为什么这个实现无效?

function - 递增值的函数如何工作?