为什么函数重载会修复类型缩小问题?



我遇到了一个问题,它基本上涉及缩小从对象返回的类型,通过其键访问。假设我有一个对象,其值可以是numberstring

type Value = number | string;
enum DatumKey {
numberDatum = 'numberDatum',
stringDatum = 'stringDatum',
}
class DemoClass {
private data = {
[DatumKey.numberDatum]: 1337,
[DatumKey.stringDatum]: 'foobar',
}
public getValue(key: DatumKey): Value {
return this.data[key];
}
public doSomething(): void {
const num: number = this.getValue(DatumKey.numberDatum); // TS expects `number | string`, I expect `number`
const str: string = this.getValue(DatumKey.stringDatum); // TS expects `number | string`, I expect `string`
console.log({ num, str });
}
}
const dc = new DemoClass();
dc.doSomething();

请参阅 Typescript Playground 示例。


失败的方法

因此,为了缩小类型范围,我尝试使用泛型类型,然后我可以告诉getValue<...>()我期望返回哪种类型:

public getValue<T extends Value>(key: DatumKey): T {
// Error here: Type 'Value' not assignable to type 'T'
return this.data[key];
}
public doSomething(): void {
const num: number = this.getValue<number>(DatumKey.numberDatum); // TS expects `number`, it's good!
const str: string = this.getValue<string>(DatumKey.stringDatum); // TS expects `string`, it's good!
}

但是,通过这样做,我从TypeScript收到一个错误:

Type 'Value' is not assignable to type 'T'.
'Value' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
Type 'string' is not assignable to type 'T'.
'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.

请参阅 Typescript Playground 示例。


工作方式

然后我在这里遇到了一个类似的问题,它建议使用函数重载来解决问题。确实如此。但是,我不确定为什么函数重载可以解决此问题:

public getValue<T extends Value>(key: DatumKey): T 
public getValue(key: DatumKey): Value {
return this.data[key];
}
public doSomething(): void {
const num = this.getValue<number>(DatumKey.numberDatum); // TS expects `number`, it's good!
const str = this.getValue<string>(DatumKey.stringDatum); // TS expects `string`, it's good!
}

请参阅 Typescript Playground 示例。

您的工作解决方案实际上不起作用。变量numstr的类型为Value,而不是分别键入numberstring

更强大的解决方案:

enum DatumKey {
numberDatum = 'numberDatum',
stringDatum = 'stringDatum',
}
const data = {
[DatumKey.numberDatum]: 1337,
[DatumKey.stringDatum]: 'foobar',
};
function getValue<T extends keyof typeof data>(key: T): (typeof data)[T] {
return data[key];
}
// You don't have to explicitly type num and str. They are automatically inferred 
const num = getValue(DatumKey.numberDatum);
const str = getValue(DatumKey.stringDatum);

最新更新