interface CustomResponse {
data: string;
status: number;
[key: string]: string | number;
}
const RESPONSE_PROPS = {
DATA: "data",
STATUS: "status",
};
const response: CustomResponse = {
data: "test",
status: 200,
};
let dataWrong: string = response[RESPONSE_PROPS.DATA];
let dataRight: string = response.data;
dataWrong gets the error
Type 'string | number' is not assignable to type 'string'. Type 'number' is not assignable to type 'string'
In cases like above, how to get dataWrong infer right type in typescript? Is type assertion (better type guards) the only way?