Reputation: 2483
I want to have a function which takes some object and returns its x
attribute. The object needs to be restricted to a generic type Type<X>
and I want the type of the return value to be the type of the attribute x
.
To restrict the input to Type<X>
I need to use T extends Type<X>
but I must actually set the X
to some type value like T extends Type<string>
which won't work with Type<number>
or to T extends Type<any>
which discards the type information of the x
attribute.
I was hoping to do something like <T extends Type<any>>(o: T) => T.X
or <T extends Type<???>>(o: T) => typeof o
.
Is there a way in TypeScript to do this? If so, how?
// 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)
Upvotes: 3
Views: 1159
Reputation: 164467
If I understand you correctly then:
function getX<T>(o: Type<T>): T {
return o.x;
}
Then:
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'
Upvotes: 2