Back to questions
JuniorTheoryCommon
What does the program print?
const stock: { [item: string]: number } = { apple: 5 };
console.log(stock["pear"] + 1);NaN
- Expecting tsc to catch the missing key: an index signature allows any string key and types every read as
number, even for a key that is not there. - Expecting a missing key to read as
0: JavaScript givesundefined, andundefined + 1isNaN.
- Which compiler option makes
stock["pear"]typednumber | undefined? - How would storing the stock in a
Mapchange the type of a read by key?