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.
shellgit clone https://github.com/LearningTypeScript/projects learning-typescript-projectscd learning-typescript-projectsnpm i
shellgit clone https://github.com/LearningTypeScript/projects learning-typescript-projectscd learning-typescript-projectsnpm i
Open your editor in this project's directory:
shellcode projects/generics/treasure-hunter
shellcode projects/generics/treasure-hunter
In one terminal, run the TypeScript compiler via the tsc script.
For example, to start the TypeScript compiler in watch mode:
shellnpm run tsc -- --watch
shellnpm run tsc -- --watch
In another terminal, run Jest via the test script.
For example, to start tests in watch mode:
shellnpm run test -- --watch
shellnpm run 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: ABuriedobject (see later) ofContentdataisFake: A type predicate function that takes in adatumand returns whether it isFakeisReal: A type predicate function that takes in adatumand returns whether it isReal
Also create and export a Buried type with a single type parameter.
Each Buried object can be one of three things:
- An array of the same type of
Buriedobjects - A
NextAreaobject, which can be either:- A
Catacombshape, with properties:inside: ABuriedobject of the same typetype:"catacomb"
- A
TunnelSystemshape, with properties:entrances: An array ofBuriedobjects of the same typetype:"tunnels"
- A
- A
Treasureobject with properties:content: Adatumof the same typetype:"treasure"
The collectTreasure function should return an object with three properties:
fake: Array of foundFakeitemsreal: Array of foundRealitemsscrap: Array of all other items
Notes
Note: your terminal should be in the
treasure-hunterdirectory, not the root repository's directory.
- Don't use
anyor leave any implicitanys.