I'm trying to do a type inference function in typescript inside a reactive bus class.
Here is an example:
// This is the function
getValue<T>(data: T, key: keyof T) {
return data[key];
}
// This is the test
interface IState {
field1: number;
field2: string;
}
const state: IState = {
field1: 123,
field2: 'abc'
};
const x = getValue(state, 'field1');
Key variable is infered succesfully (i cannot type different values than interface keys). The problem is that doing this the type of 'x' variable is number|string but i'm expecting number.
Am i missing something? Is it possible?
Thank you!