2017-08-10 102 views
3

我想要一個函數,它需要一些對象並返回它的x屬性。該對象需要被限制爲通用類型Type<X>,我希望返回值的類型是屬性x的類型。TypeScript中泛型的子類型的推斷

要限制輸入Type<X>我需要使用T extends Type<X>但我必須實際設置XT 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) 

回答

2

如果我理解你正確,則:

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' 

code in playground

+0

是啊,你理解正確,這正是我想達到的,但這只是我沒有想到的。 –