I would like to find a way in Typescript to create a hashmap with indexable types that includes a warning when the value could potentially be undefined
during a lookup. Is there a solution for this issue?
interface HashMap {
[index: number]: string;
}
const exampleMap: HashMap = {
1: "foo",
2: "bar"
}
const noWarningHere = exampleMap[3];
console.log(noWarningHere.length);
Currently, running this code gives a TypeError: noWarningHere is undefined in the browser console without any complaints from the TypeScript compiler. Typically, the compiler would indicate that the value could be undefined and suggest creating a guard clause.