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

Maybe

The following Maybe data type is a Functor and an Applicative, which means it contains a value and implements the map method. The main difference with the preceding implementation of Functor is that in the Maybe Functor, the value contained by the data type is optional:

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)); 
        } 
    } 
} 

As we can see in the preceding implementation of the map method, the mapping ...

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