import { match, strike, otherwise } from'matchbook';
// Get the level of Personal Protective Equipment for each careerclass Human { }
class Nurse extends Human { }
class ConstructionWorker extends Human { }
class BombSquadTechnician extends Human { }
const actual = strike(
new Nurse(),
match(Nurse, PpeLevel.Low),
match(ConstructionWorker, PpeLevel.Medium),
match(BombSquadTechnician, PpeLevel.High),
otherwise(Ppe.None),
);
assertEq(actual, PpeLevel.Low);
import { match, strike, otherwise } from'matchbook';
const actual = strike<unknown, string>(
4,
match(
n =>typeof n === 'number',
// NOTE: Even though we're matching on type unknown,// since our type guard is satisfied,// this delegate knows n is a number.
n => `sqrt: ${Math.sqrt(n)}`
),
otherwise('no clue')
);
assertEq(actual, 'sqrt: 2');
Match if
val
is an instance of a Constructor1.0.0
import { match, strike, otherwise } from 'matchbook'; // Get the level of Personal Protective Equipment for each career class Human { } class Nurse extends Human { } class ConstructionWorker extends Human { } class BombSquadTechnician extends Human { } const actual = strike( new Nurse(), match(Nurse, PpeLevel.Low), match(ConstructionWorker, PpeLevel.Medium), match(BombSquadTechnician, PpeLevel.High), otherwise(Ppe.None), ); assertEq(actual, PpeLevel.Low);