Reading Error Messages
All of TypeScript's error messages look alike, and once you can read them, they tell you a lot. This program has three mistakes:
const hero = { name: "Kira", health: 50 };
let potions = 2;
console.log("Health:", hero.helth);
console.log("Potions:", potoins);
console.log("Arrows:", arrows);TypeScript reports all three at once, one per line:
main.ts(4,29): error TS2551: Property 'helth' does not exist on type '{ name: string; health: number; }'. Did you mean 'health'?
main.ts(5,25): error TS2552: Cannot find name 'potoins'. Did you mean 'potions'?
main.ts(6,24): error TS2304: Cannot find name 'arrows'.Read each line from the left:
main.tsis the file with the mistake, the one in the editor.(4,29)is the place: line 4, column 29. You can see the line numbers on the left of the editor. The column says which character on the line it is, counting from 1, spaces included. TypeScript points at the start of what is wrong, here the first letter ofhelth.error TS2551says that it's an error, andTS2551is its code. Every kind of error has its own number. When a message isn't clear to you, search the internet for its code, for example “TS2551”, and you'll find explanations and examples.- After the colon comes a description in English.
Property 'helth' does not exist on type '{ name: string; health: number; }'means that the object has no propertyhelth. The part in quotes is the object's type: an object with a propertyname, which is a text (string), and a propertyhealth, which is a number. TypeScript worked it out from the object on line 1.
When a wrong name is similar to one that exists, TypeScript adds a suggestion, Did you mean 'health'?, and it's usually the right fix. Suggestions come with properties (TS2551) and with variables too: Cannot find name 'potoins' (TS2552) means that TypeScript doesn't know any variable potoins, but it knows potions. When no name is similar, you get just Cannot find name 'arrows' (TS2304): the program has no variable arrows. Maybe you forgot to create it, or you named it differently.
TypeScript checks the whole program and reports every type error it finds. Start with the first one: fix it and run the program again. One mistake can cause several messages. When you misspell a variable's name where you create it, every line that uses the variable reports an error, and a single fix makes them all disappear.
And the other way round: after a fix, new errors can appear. Before the types, TypeScript checks that the program is written correctly, with all the parentheses and quotes in place. When something like that is wrong, TypeScript reports only that, and it doesn't check the types until you fix it:
const hero = { name: "Kira", health: 50 };
console.log("Health:", hero.health;main.ts(2,35): error TS1005: ')' expected.')' expected means that TypeScript expected a ) there, at the semicolon, because the parenthesis of console.log( wasn't closed.
Instructions
The program in the editor welcomes Kira to a tavern, but it has three mistakes. Run it: TypeScript reports just one of them, a mistake in how the program is written. Fix it and run the program again. Now TypeScript shows the other two. Fix them as well, so that the program prints:
Welcome to The Golden Goblet
Your host: Bruno
Kira orders a soup. Gold left: 105Start programming for free
3/7