我不明白为什么在这个最小的可重现示例中类型断言不起作用。
type Letter = "A" | "B"
type Useless = {}
type Container<T> =
Useless |
{
type: "container"
value: T
}
function transform<X extends Letter, Y extends Letter>(container: Container<X | Y>): asserts container is Container<X> {
// custom logic
}
let container: Container<"A" | "B"> = undefined as any
// Asserts no B
transform<"A", "B">(container)
container // Container<"A" | "B">
// Container<"A"> when Useless is commented
// I want Container<"A">
我希望容器 的类型为Container<"A">
。
当我完全删除类型 Useless 时,它会按预期工作。
当我用type Useless = { key: string }
替换 Useless 类型时,我的容器的类型是 Useless 而不是Container<"A">
。
如何获取类型Container<"A">
?
答案 0 :(得分:0)