typescript · level 2 · unit 1 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
An keeps related facts together under one name.
So far each box held one value. Real data comes in bundles: a pet has a name, an age and a favourite toy. An object bundles them in curly brackets, like { name: "Rex", age: 3 }. Each part is a property: a name, a colon and a value.
Read a property with a dot, like rex.name, the same dot you used for .length. Change one the same way: rex.age = 4;. Even in a const box the properties can change. Only the box itself is locked.
The type label for an object lists each property and its type: { name: string; age: number }. Ask for a property that is not on the list and the checker stops you with TS2339.
const rex: { name: string; age: number } = { name: "Rex", age: 3 };
console.log(rex.name);
rex.age = 4;
console.log(`${rex.name} is now ${rex.age}.`);
console.log(rex); When you show a whole object, each property name appears in quotes, with no spaces.
guess first
One property changes. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
const film = { title: "Up", minutes: 96, seen: true };
film.seen = false;
console.log(film.title, film.minutes);
console.log(film);
your turn
Your turn. Describe a snack.
Fill in the snack's name, its price in pounds and how many you have left. Then run it.
fix it
The checker cannot find the colour. Can you fix it?
- Read the error:
TS2551: Property 'color' does not exist. The checker even suggests what you meant. - Compare the property name on line 2 with the names in the object on line 1. One letter is missing.
- Change
bike.colortobike.colouron line 2, then press run.
nailed it
your data comes in tidy bundles now. :D
- An object keeps related properties together in curly brackets.
- Read or change a property with a dot, like
rex.age. - The checker refuses properties that are not in the type.
new words in your collection
- object
This lesson isn’t saved. A free account remembers every lesson you finish.