3
我想要一个函数,它需要一些对象并返回它的x
属性。该对象需要被限制为通用类型Type<X>
,我希望返回值的类型是属性x
的类型。TypeScript中泛型的子类型的推断
要限制输入Type<X>
我需要使用T extends Type<X>
但我必须实际设置X
像T extends Type<string>
某种类型的值不会Type<number>
或T extends Type<any>
其丢弃x
属性的类型信息的工作。
我希望能做一些像<T extends Type<any>>(o: T) => T.X
或<T extends Type<???>>(o: T) => typeof o
。
在TypeScript中有办法做到这一点吗?如果是这样,怎么样?
// Suppose I have this generic interface
interface Type<X> {
readonly x: X
}
// I create one version for foo...
const foo: Type<string> = {
x: 'abc',
}
// ...and another one for bar
const bar: Type<number> = {
x: 123,
}
// I want this function to restrict the type of `o` to `Type`
// and infer the type of `o.x` depending on the inferred type of `o`,
// but to restrict to `Type` I must hardcode its X to `any`, which
// makes `typeof o.x` to evaluate to `any` and the type of `o.x` is lost.
function getX<T extends Type<any>> (o: T): typeof o.x {
return o.x
}
// These are correctly typed
const okFooX: string = getX(foo)
const okBarX: number = getX(bar)
// These should result in error but it is OK due to `Type<any>`
const errorFooX: boolean = getX(foo)
const errorBarX: boolean = getX(bar)
是啊,你理解正确,这正是我想达到的,但这只是我没有想到的。 –