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.
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/classes/the-shape-of-types
shellcode projects/classes/the-shape-of-types
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
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 or equal to the opponent's, consumes the opponent (read more later).getPower: Returns the sum of callingthis.getPowerFromon 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: Thenameof 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
nameto set as the property, and anevilboolean - Methods:
getPowerFrom: If the previously consumed horror'sevilmatches this sorcerer's evil, returns double its power; otherwise, returns exactly its powerisEvil: Returns theevilboolean received in the constructor
- Constructor: takes in a
Notes
Note: your terminal should be in the
the-shape-of-typesdirectory, not the root repository's directory.
- You may add
#private members to the classes as you wish, such as to store previously consumed opponents.