Generic Interfaces with Functions
In this exercise we have a large file, so we'll start with a tour.
Cache interface that takes in a
T and includes a getter and setter that we're interested in.
0:00 This exercise is pretty big file here, but we're only interested in a small part of it. Let me explain the big file first. We have a Cache interface here, which takes in a T and it's like a getter and setter here. This is the piece that we're interested in but let me carry on to the implementation.
0:19 This createCache here, we can get it, we can set it. You can see here that this T, the actual cache itself is a record of strings with T in it, which you can pass in initialCache and then it returns the Cache.
0:37 Down here, you create the cache with number, you can cache.set("a"), but you can't pass in anything that's not in the type that's in the cache there. I can set('c') and set it to string. That's going to yell at me, because it should be a number. It will yell at me when I restart my TS Server. I'm sure. Give it three, two, yeah, there it is.
1:00 After this, there's a function that we've added here called clone. The entire fix for this is on this one line here. What clone is supposed to do is it takes in a cache, and it takes in a transform function for that cache.
1:20 We have our numberCache set up here. What this is supposed to do is it supposed to take that cache and clone it, and for each member of the cache, run this function. In this function, what it's going to do is take the element, which is supposed to be a number in this particular cache, and then stringify it.
1:40 What we should end up with is a stringCache here. The stringCache currently, a is equal to unknown here, but it should be string or undefined. The stringCache should be Cache<string>, not Cache<unknown>.
1:54 You can take a look here. There's an any in here, which you don't need to mess with, you don't need to remove. This is saying...I mean, this is interesting. Maybe you can fiddle around with this. I might fiddle around with it in the solution, but this line is the interesting piece here.
2:10 The things you'll need to solve these are an understanding of how generics work on different function levels, which we have done in the previous exercise, and also, to be able to type these functions properly, and understand how this T flows through the types here.
2:26 Good luck.