Beginner's TypeScript Section 18 exercises
Problem

Assigning Dynamic Keys to an Object

Consider this createCache function:


const createCache = () => {
const cache = {};
const add = (id: string, value: string) => {
cache[id] = value;
};
const remove = (id: string) => {
delete cache[id];
};
return {
cache,
add,
remove,
};
};

We'

Loading exercise

Transcript

0:00 Inside this createCache function, we're creating an object, which we're calling a cache, and allowing users to specify add and remove on that cache. Inside here, you can see we've got cache createCache add 123 Matt, and then there's some errors that are happening here because it looks like cache is just typed as this. It's like an empty object, which doesn't seem quite correct.

0:27 We're getting errors up here, too. "No index signature with a parameter of type string was found on type..." What? OK, some confusing stuff happening here and all to do with index signatures. We can't index it there. There's no indexing happening here.

0:45 Your job is to go into the TypeScript docs and work out what could be causing this problem and how we should type this line of code here to make these errors go away.