I am struggling with a code snippet where I am trying to check if a string exists in my custom record using the if(x in obj)
guard statement, but it seems to not be working as expected.
Below is the sample code snippet that is throwing an error:
type Answer = 'yes' | 'no';
type ToDo = Partial<Record<Answer, string>>;
function doFunction(toDo: ToDo, hisAnswer: string): string {
if (hisAnswer in toDo) {
return toDo[hisAnswer];
// ^--- Error!
// Element implicitly has an 'any' type because expression of type
// 'string' can't be used to index type 'Partial<Record<Answer, string>>'.
// No index signature with a parameter of type 'string'
// was found on type 'Partial<Record<Answer, string>>'.ts(7053)
}
}