如何在typescript中分配泛型条件推理类型的值



我试图使这些条件推断类型起作用:

type A<T> = T extends { a: infer U } ? U : never
type B<T> = T extends { a: infer U } ? U : never
class Test<K extends { a: string }> {
constructor(props: string) {
// Error: string is not assignable to A<K>
let a: A<K> = props
// Error
let b: B<K> = props
// Error: B<K> is not assignable to A<K>
a = b
}
}

A<K>B<K>具有相同的定义,但不能相互分配。我不能理解A<K>B<K>在编译时都应该是字符串类型,但不能接受string的值
有什么办法让它发挥作用吗?

正如@jcalz所说,TS3.7修复了它。

最新更新