Can you explain why the
stopsDict["first"].directions.push("test");
line is successful, but not the stopsDict[stopName].directions.push("test");
one?
interface StopsDict {
[key: string]: Stops;
}
interface Stops {
directions?: string[];
}
let stopsDict: StopsDict = {
first: {
directions: []
},
second: {}
};
if (Array.isArray(stopsDict["first"].directions)) {
stopsDict["first"].directions.push("test"); //Operation Success
}
let stopName = "first";
if (Array.isArray(stopsDict[stopName].directions)) {
stopsDict[stopName].directions.push("test"); //error TS2532: Object is possibly 'undefined'.
}