Treasure Hunter
A Learning TypeScript > Generics 🍲 entree project.
Good evening, professor. We're with the National Typeological Survey. We've received word that there is a new system of catacombs and tunnels beneath your city. We'd like you to explore them.
Please submit a collectTreasure
function to us that can recursively sift through buried contents.
We will provide you functions to determine what is fake, real, or general scrap.
Setup
If you haven't yet, set up the github.com/LearningTypeScript/projects repository locally.
git clone https://github.com/LearningTypeScript/projects learning-typescript-projects
cd learning-typescript-projects
npm i
Change your terminal directory to this project's:
cd projects/generics/treasure-hunter
In a terminal, start the TypeScript compiler in watch mode:
tsc --watch
In another terminal, run Jest via the test
script.
For example, to run tests in watch mode:
npm test -- --watch
Specification
Export a collectTreasure
function.
It should have three type parameters: Content
, Fake
, and Real
.
Fake
and Real
should both extend Content
.
The collectTreasure
function should have three runtime parameters:
buried
: ABuried
object (see later) ofContent
dataisFake
: A type predicate function that takes in adatum
and returns whether it isFake
isReal
: A type predicate function that takes in adatum
and returns whether it isReal
Also create and export a Buried
interface with a single type parameter.
Each Buried
object can be one of three things:
- An array of the same type of
Buried
objects - A
NextArea
object, which can be either:- A
Catacomb
shape, with properties:contents
: ABuried
object of the same typetype
:"catacomb"
- A
TunnelSystem
shape, with properties:tunnels
: An array ofBuried
objects of the same typetype
:"tunnels"
- A
The collectTreasure
function should return an object with three properties:
fake
: Array of foundFake
itemsreal
: Array of foundFeal
itemsscrap
: Array of all other items
Notes
- Don't use
any
or leave any implicitany
s.