Skip to Content
Learning TypeScript 2.x - Second Edition
book

Learning TypeScript 2.x - Second Edition

by Remo H. Jansen
April 2018
Beginner content levelBeginner
536 pages
13h 21m
English
Packt Publishing
Content preview from Learning TypeScript 2.x - Second Edition

Monad

We are going to finish our introduction to algebraic data types by learning about monads. A Monad is a Functor, but it also implements the Applicative and Chain specifications.

We can transform the previously declared Maybe data type into a Monad by adding two extra methods named join and chain:

class MayBe<T> { public static of<TVal>(val?: TVal) { return new MayBe(val); } private _value!: T; public constructor(val?: T) { if (val) { this._value = val; } } public isNothing() { return (this._value === null || this._value === undefined); } public map<TMap>(fn: (val: T) => TMap) { if (this.isNothing()) { return new MayBe<TMap>(); } else { return new MayBe<TMap>(fn(this._value)); } } public join() { return this.isNothing() ? Nothing.of(this._value) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering TypeScript - Fourth Edition

Mastering TypeScript - Fourth Edition

Nathan Rozentals
Learning TypeScript

Learning TypeScript

Josh Goldberg
TypeScript for Beginners

TypeScript for Beginners

Bharath Thippireddy

Publisher Resources

ISBN: 9781788391474Supplemental Content