Build TypeScript generics: 12 levels of T, extends, infer and mapped types. The most in-depth advanced TS generics game around.
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.
T, that takes the shape of the goods. Which mould does giling(kopi) on line 6 use?giling(beras) on line 8 use?contoh takes a tray T[] and gives back one thing from it. Which mould T for contoh(cabai)?bungkus puts the goods into a crate { isi: T }. Which mould T for bungkus(pedas)?gabung has two hoppers, A and B, and puts the two together. Pick the moulds A and B for gabung(bumbu, takaran).olah changes what is on the tray one by one. Pick the moulds T and U for olah(karung, (kg) => kg + " kg").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.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.ambil takes a key K limited to keyof Kopi. Which mould K for ambil("harga")?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.Wadah<T> picks the container. Write Wadah<T> so text comes out to "kantong" and the rest to "karung".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[].