Missing and Extra Properties
When a variable has a type, TypeScript checks every object you put into it, property by property. Two mistakes come up all the time: a property is missing, or there's one too many. First, a missing one:
type Item = { name: string; price: number };
const rope: Item = { name: "Rope" };main.ts(3,7): error TS2741: Property 'price' is missing in type '{ name: string; }' but required in type 'Item'.Read it like this: the object you wrote has the type { name: string; }, but Item also requires price. In JavaScript, the rope would get into the shop without a price, and the total of a purchase would come out as NaN. When several properties are missing, the message lists them all, for example Type '{ name: string; }' is missing the following properties from type 'Item': price, weight (TS2739).
Now an extra property:
type Item = { name: string; price: number };
const rope: Item = { name: "Rope", price: 5, weight: 2 };main.ts(3,46): error TS2353: Object literal may only specify known properties, and 'weight' does not exist in type 'Item'.An object literal is an object written right in the code, in curly braces. An extra property in it usually means one of two things. Either the type lacks a property that you need, and you add it to the type, or the property doesn't belong in the object, and you delete it.
Very often, it's a typo. TypeScript then guesses what you meant:
type Item = { name: string; price: number };
const rope: Item = { name: "Rope", prise: 5 };main.ts(3,36): error TS2561: Object literal may only specify known properties, but 'prise' does not exist in type 'Item'. Did you mean to write 'price'?The extra properties are checked only in object literals. An object that already exists in another variable may have more properties than the type needs. TypeScript only requires that it has all the properties of the type:
type Item = { name: string; price: number };
const loot = { name: "Rope", price: 5, weight: 2 };
const rope: Item = loot;
console.log(rope);This prints:
{ name: "Rope", price: 5, weight: 2 }This is how TypeScript thinks about objects in general: an object is an Item when it has everything an Item needs.
Instructions
Fix the two monsters that TypeScript reports. The troll has a typo, and the goblin is missing its damage: it deals 3. The program prints:
wolf: 30 health, 6 damage
troll: 80 health, 12 damage
goblin: 20 health, 3 damageA spider comes to the forest. Below the goblin, add const spider: Monster = { name: "spider", health: 15, damage: 4, reward: 8 }; and, at the end, print describe(spider). TypeScript reports reward, which monsters don't have. This time, it's the type that lacks something: add reward: number to Monster. TypeScript then reports the other three monsters, because they have no reward. Give them one: 10 for the wolf, 40 for the troll and 5 for the goblin. The program then also prints spider: 15 health, 4 damage.
At the end of the program, print the reward for all four monsters together, by adding up their reward properties:
Reward: 63 goldStart programming for free
3/10