说我有此接口和包含该类型的对象:
interface IMyData {
a: TypeA;
b: TypeB;
c: boolean;
d: string;
}
const myObj: { data: IMyData } = {
data: {
a: someValueA,
b: someValueB,
c: true,
d: "someValueD"
}
}
现在,我想从该对象获取单个属性,并让该函数推断返回类型:
function getField(obj: { data: IMyData }, field: keyof IMyData){
return obj.data[field];
}
const myFieldStr = getField(myObj, "d"); // should infer type string
const myFieldBool = getField(myObj, "c"); // should infer type boolean
如何定义getField
函数,以便推断返回类型?现在,它将推断TypeA | TypeB | boolean | string
。
这是另一种(更复杂的情况):
interface IMyValue<T> {
value?: T;
}
interface IMyData2 {
a?: IMyValue<string>;
b?: IMyValue<number>;
c?: IMyValue<boolean>;
d?: IMyValue<string>;
}
function getValue<T extends keyof IMyData2>(field: T, data: IMyData2) {
return data[field] ? data[field]!.value : undefined; // this wouldn't compile without '!' mark
}
const testData: IMyData2 = {
a: { value: 'a' },
b: { value: 2 },
c: { value: false },
};
const testValue1 = getValue('a', testData); // i want this to detect as type of `string`
const testValue2 = getValue('b', testData); // i want this to detect as type of `number`
const testValue3 = getValue('b', testData); // i want this to detect as type of `boolean`
const testValue4 = getValue('b', testData); // i want this to detect as type of `undefined`
答案 0 :(得分:3)
您必须告诉类型系统field
的确切字面值。最简单的方法是使用如下通用函数:
interface IMyData {
c: boolean;
d: string;
}
const myObj: { data: IMyData } = {
data: {
c: true,
d: "someValueD"
}
}
function getField<T extends keyof IMyData>(obj: { data: IMyData }, field: T){
return obj.data[field];
}
const myFieldStr = getField(myObj, "c"); // should infer type boolean
const myFieldBool = getField(myObj, "d"); // should infer type string
或者在最简单的一般情况下:
function pluck<T, K extends keyof T>(obj : T, key : K) {
return obj[key];
}
const foo = pluck({ bar: "asd", baz: 5 }, "bar"); // infers string