[Typescript] UnionBuilder

interface UnionBuilder<T = never> {
    add: <NewValue>() => UnionBuilder<T | NewValue>,
    fold: () => T
}

declare const u: UnionBuilder;

const result = u
    .add<string>()
    .add<number>()
    .add<boolean>()
    .fold() // string | number | boolean

 

posted @ 2023-12-27 22:21  Zhentiw  阅读(10)  评论(0编辑  收藏  举报