Pattern matching is a fundamental and powerful building block to many functional programming languages like Haskell or Scala.
(In case you have never heard anything about pattern matching, think about it like a type safe switch statement on steroids.)
TypeScript (and with that, JavaScript) has no pattern matching functionality built in.
Following code introduces an interface containing cases to handle possible values of the boolean type. Each case is a function which will return a T.
interface BooleanPattern<T> { True: () => T, False: () => T }The BooleanPattern allows us to describe specific behaviours for each possible case.
Following matchBoolean function takes such behaviour description and returns a matcher. function which then will execute the matching case by
function matchBoolean<T>(p: BooleanPattern): (b: boolean) => T { return (b: boolean) => { if (b) { return p.True(); } return p.False(); } }This could be used:
const loggedIn: boolean = await isUserLoggedIn(); const result = matchBoolean({ True: () => 'User is logged in', False: () => 'User is logged out' })(loggedIn);