Back to questions
JuniorTheoryCommon
What does tsc report for this program?
const scores = [90, 75];
scores.push("100");
console.log(scores.length);An error: a string cannot be added to the number[] array
- Expecting
pushto accept any value, as in JavaScript: TypeScript infersnumber[]from[90, 75], and a string does not fit its parameter. - Expecting
constto stop the array from growing:constprotects only the name, soscores.push(100)would compile without an error.
- How would you declare
scoresso that it may hold both numbers and strings? - Which type does the property
itemsget inconst cart = { items: [] }, and why?