【问题标题】:How to define a recursively string literal type in Typescript如何在 Typescript 中定义递归字符串文字类型
【发布时间】:2023-03-21 07:43:01
【问题描述】:

我想为字符串中以':'分隔的一系列数字定义一个类型 示例:'39:4893:30423'、'232'、'32:39'

所以这是我尝试过的:

type M = `${number}` | '' 
type ML = `${M}` | `${M}:${ML}` 
// ERROR : Type alias 'ML' circularly references itself.

为什么我不能这样做? 你有可行的替代方案吗?

【问题讨论】:

    标签: typescript recursion string-literals compile-time circular-reference


    【解决方案1】:

    这篇文章有一些关于字符分隔字符串的有趣讨论,可能符合您的需求:Define a Typescript string type of comma separated union types?

    【讨论】:

    • 感谢您的指导。就我而言,我不能为所有数字和无限的数字列表做到这一点
    猜你喜欢
    • 2020-12-11
    • 2019-10-09
    • 2017-08-17
    • 2016-07-22
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    相关资源
    最近更新 更多