Objects 16 exercises
Problem

Allow Any String Key while Supporting Default Properties

Here we have a scores object with default properties for math and english:


interface Scores {}
// @ts-expect-error science is missing! // red squiggly line
const scores: Scores = {
math: 95,
english: 90,
};

The @ts-expect-error comment tells TypeScript to expect an error, w

Loading exercise

Transcript

00:00 In this exercise, we have our scores object with a couple of default properties in it, math and English. We want to be able to kind of like assign anything to this, so similar to the previous exercise, we want to be able to add any string key and add a number, but we actually want three default keys in here.

00:19 So we want math, English and science. Each of those are going to be numbers. Your job is to try to figure out the correct typing for this interface of scores. I've set this as an interface, but you can change it to a type if you like, I don't mind. You will need an index signature and we'll also probably need some predefined keys as well.

00:37 Your job is to figure out how to type that correctly. Good luck.