Generic Machine — TypeScript

Build TypeScript generics: 12 levels of T, extends, infer and mapped types. The most in-depth advanced TS generics game around.

What you learn in this game

A generic is one function for many types. T in giling<T> is an empty mould: when the function is called, TypeScript fills it with the type of the argument going in, and that same type comes out.

One generic function can be called many times with different goods. T is filled again at every call, so the first call and the second may have different moulds.

If the parameter is T[], T is the type of each item of the array, not the array's type. A function that returns T gives back one item.

T can be used inside the return type. If a function returns { isi: T }, the crate that comes out carries the type of the goods that went in.

A generic function may have more than one type parameter. Each type parameter is filled from its own argument, and A & B puts the two together.

With two type parameters a function can change the type: T for what goes in, U for what comes out. tsc fills U from the value the callback gives back.

What the levels ask

  1. Kak Piksel brings robusta coffee for Sunday's harvest thanksgiving. The mill has one mould, T, that takes the shape of the goods. Which mould does giling(kopi) on line 6 use?
  2. Bu Guru Ratna brings tubruk coffee for the teachers' room and 2 kilos of rice for cake flour. The same machine. Which mould does giling(beras) on line 8 use?
  3. Mak Ijah brings a tray of chillies for the sambal and wants to taste the first one. The machine contoh takes a tray T[] and gives back one thing from it. Which mould T for contoh(cabai)?
  4. Bu Sari packs the hot mark of her sambal: more than 10 chillies means hot. The machine bungkus puts the goods into a crate { isi: T }. Which mould T for bungkus(pedas)?
  5. Bu Yanti writes her order on two slips: the spice's name and its weight. The machine gabung has two hoppers, A and B, and puts the two together. Pick the moulds A and B for gabung(bumbu, takaran).
  6. Pak Harun carries the rice sacks to the hall and wants every sack labelled with its weight. The machine olah changes what is on the tray one by one. Pick the moulds T and U for olah(karung, (kg) => kg + " kg").
  7. Dina makes the labels for the feast: the coffee's name in capitals, the rice's weight with one decimal. But the machine still uses the old mould, unknown: everything that comes out loses its shape. Rewrite the signature of giling with a type parameter, so what comes out is what went in.
  8. Pak RT wants to know which is longer: his announcement, or the feast's shopping list. The machine lebihPanjang measures .length, but tsc does not know T has a length. Write a limit for T so text and lists can be measured, and numbers are refused.
  9. Rara wants to haggle over the coffee, so she asks the machine to open the price drawer of the stock cupboard. The machine ambil takes a key K limited to keyof Kopi. Which mould K for ambil("harga")?
  10. Bang Jaya changes his order: only the weight, to 5 kilos. The machine ubah asks for whole new goods, but Bang Jaya brings only one drawer. Write the type of baru so any drawer may be brought on its own, and a drawer that does not exist is refused.
  11. Pak Karim takes the ground goods to the island in his boat: powder goes in a bag, grain in a sack. The gate Wadah<T> picks the container. Write Wadah<T> so text comes out to "kantong" and the rest to "karung".
  12. Mas Bayu counts the feast's budget to the last rupiah. The machine catat takes one drawer from every item in stock, but forgets which drawer: the prices come out as (string | number)[]. Write the signature of catat so the prices come out as number[] and the names as string[].