TypeScript 中泛型子类型的推断
Inference of Subtypes of Generic Types in TypeScript
我想要一个函数,它接受一些对象和 return 它的 x
属性。该对象需要限制为通用类型 Type<X>
并且我希望 return 值的类型是属性的类型 x
.
要将输入限制为 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)
如果我理解正确的话:
function getX<T>(o: Type<T>): T {
return o.x;
}
然后:
const errorFooX: boolean = getX(foo); // error: Type 'string' is not assignable to type 'boolean'
const errorBarX: boolean = getX(bar); // error: Type 'number' is not assignable to type 'boolean'
我想要一个函数,它接受一些对象和 return 它的 x
属性。该对象需要限制为通用类型 Type<X>
并且我希望 return 值的类型是属性的类型 x
.
要将输入限制为 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)
如果我理解正确的话:
function getX<T>(o: Type<T>): T {
return o.x;
}
然后:
const errorFooX: boolean = getX(foo); // error: Type 'string' is not assignable to type 'boolean'
const errorBarX: boolean = getX(bar); // error: Type 'number' is not assignable to type 'boolean'