【发布时间】:2023-02-21 18:33:14
【问题描述】:
我有一个简单的通用接口 W 和两个接口 T 和 N 扩展 W 并添加一个属性 type 可以在标记联合中使用:
interface W<V> {
value: V
}
interface T extends W<string> {
type: 'text'
}
interface N extends W<number> {
type: 'number'
}
此外,我有一个类型 D,它是 T 和 N 的联合,还有一个函数 getValue,它需要一个符合通用包装器类型的参数,并简单地返回它的包装值。
type D = T | N
const getValue = <V extends any>(
wrapper: W<V>
): V => {
return wrapper.value
}
我的问题是,如果我创建一个类型为D 的值并将其传递给getValue,tsc 会抱怨the argument of type 'D' is not assignable to parameter of type 'W<string>':
// typecast necessary because otherwise tsc would determine that d is of type 'T' which is what i don't want
const d: D = { value: 'hallo', type: 'text'} as D
// why is 'D' not an acceptable type for getValue??? Shouldn't the inferred return type simply be 'string | number'?
getValue(d)
我尝试以这样的方式键入函数getValue,如果传入类型为D的值,tsc 将能够推断返回类型将为string | number。如果我传递值 d,我希望编译器不会抱怨并推断返回类型将是 string | number。
【问题讨论】:
标签: typescript typescript-generics