The Shape of Types
A Learning TypeScript > Classes 🍲 entree project.
Hark! Listen! The eternal deathly scrapings of a thousand locked doors rattling from unseen forces. Back. Forth. Back. Forth.
An unspeakable horror consumes all. It is here, and it is hungry.
To understand this ancient evil, we must implement it using TypeScript classes. Only then shall we have the clarity to send our sorcerers into battle against it.
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/classes/the-shape-of-types
In one 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
The first class you must export from the file must be named Horror
.
It will contain...
- Property:
name
: An abstract read-only string only visible to the class and its derived classes
- Public Methods:
doBattle
: Takes in an opponentHorror
, and if this horror's.getPower()
is greater than the opponent's, consumes the opponent (read more later).getPower
: Returns the sum of callingthis.getPowerFrom
on each previously consumed opponent (read more later), plus the number of previously consumed opponents
- Protected Abstract methods:
getPowerFrom
: Takes in a previously consumed opponent and returns a computed power numberisEvil
: Returns a boolean
Each previously consumed horror passed to getPowerFrom
should contain the following properties:
evil
: Whether the horror was evilname
: Thename
of the consumed horrorpower
: The consumed horror's previous power level
Two more exported classes must be exported that each extends Horror
:
Demon
, with:- Property:
name
:"Demon"
- Methods:
getPowerFrom
: If the previously consumed horror was evil, returns half its power; otherwise, returns double its powerisEvil
: Returnstrue
- Property:
Sorcerer
, with:- Constructor: takes in a
name
to set as the property, and anevil
boolean - Methods:
getPowerFrom
: If the previously consumed horror'sevil
matches this sorcerer's evil, returns double its power; otherwise, returns exactly its powerisEvil
: Returns theevil
boolean received in the constructor
- Constructor: takes in a
Notes
- You may add
#
private members to the classes as you wish, such as to store previously consumed opponents.