DEV Community

Why TypeScript is a better option than JavaScript when it comes to functional programming?

Remo H. Jansen on January 30, 2019

In this post, I would like to discuss the importance of static types in functional programming languages and why TypeScript is a better option than...
Collapse
 
rdeneau profile image
Romain Deneau

👍 Nice article with good examples based on a real and interesting domain.

💡 Examples can be more readable and TypeScript idiomatics this way (IMHO):

// -- Common ----

const IRELAND = Object.freeze({
    name: 'Ireland',
    ageOfMajority: 18,
});

// -- FP ----

// Helpers
type predicate<T> = (a: T) => boolean;

const either: <T>(f: predicate<T>, g: predicate<T>) => predicate<T> =
    (f, g) => a => f(a) || g(a);

const both: <T>(f: predicate<T>, g: predicate<T>) => predicate<T> =
    (f, g) => a => f(a) && g(a);

// Domain
interface Person {
    birthCountry: string;
    naturalizationDate: Date | null;
    age: number;
}

const wasBornInCountry = (person: Person) => person.birthCountry === IRELAND.name;
const wasNaturalized   = (person: Person) => Boolean(person.naturalizationDate);
const isOver18         = (person: Person) => person.age >= IRELAND.ageOfMajority;
const isCitizen        = either(wasBornInCountry, wasNaturalized);

export const isEligibleToVote = both(isOver18, isCitizen);

// -- OOP ----

class Person {
    constructor(
        // Prefer inline properties
        // Prefer no "_" prefix for private fields except to ease JavaScript initeractions
        private readonly _age: number,
        private readonly _birthCountry: string,
        private readonly _naturalizationDate: Date | null = null,
    ) { }

    // Prefer "step-down rule": outside-in declarations
    isEligibleToVote() {
        return this._isOver18() && this._isCitizen();
    }

    private _isOver18() {
        return this._age >= IRELAND.ageOfMajority;
    }

    private _isCitizen() {
        return this._wasBornInCountry() || this._wasNaturalized();
    }

    private _wasBornInCountry() {
        return this._birthCountry === IRELAND.name;
    }

    private _wasNaturalized() {
        return !!this._naturalizationDate;
    }
}
Collapse
 
mroggy85 profile image
Oskar Okuno

A other solution is to structure isEligableToVote in a more procedural way so you, at a glaze, can see what happens and what parameters are expected.
Plus add some unit tests to describe and test common use cases. And volia, your need for Typescript is gone. Of course you can still add it for static typing, but the added benifits is not worth the extra code, typing and tooling you need to introduce :)

Collapse
 
neomedeiros profile image
Leandro Medeiros

Very true

Collapse
 
itachiuchiha profile image
Itachi Uchiha

I never used TypeScript in production. So, I have no idea about TS benefits. I just used in personal projects. That does not give to me any idea about benefits.

On the other hand, we've many large applications written with Vanilla JavaScript.

For example, what should I do to see the benefits of TypeScript?

This is an important question for me and your answer will be important to understand somethings. Because I'll decide (I'm the only Front-End Developer at Work) to move our codebase to TypeScript.

Thanks :)

Collapse
 
eljayadobe profile image
Eljay-Adobe

I looked at your website, and I see that you have a broad experience with different technologies, different languages, different programming paradigms, different frameworks, and different operating systems. A kindred spirit!

If you are still doing work on the .NET platform, I think you will enjoy The Book of F# by Dave Fancher, as an introductory and tutorial book. F# is basically OCaml for .NET, by Don Syme of Microsoft in Cambridge. (I'm just a fan of the book, no vested interest. Fancher's writing style was on my wavelength for how I learn. The other F# books I've read have been disappointing. Note: I've not read Expert F# by Don Syme yet; I've got it in my book queue.)

I've used TypeScript day in, and day out, for two years on a big project at a former company. For writing industrial strength JavaScript at scale, it is a better JavaScript than JavaScript. I look forward to reading your book. :-)

Collapse
 
tycho01 profile image
Kiara Grouwstra

try composing identity function in TS. 🙁

Collapse
 
avalander profile image
Avalander

When I write libraries meant to be imported in other projects I usually write type definition files for the public interface, since some typing is still better than no typing, but I do feel that TypeScript is considerably lacking when it comes to typing functional constructs. I wish that TypeScript had brought us algebraic data types and pattern matching instead of classes and private methods.

Collapse
 
darcyrayner profile image
Darcy Rayner

Typescript doesn't have algebraic data types? It has type unions and intersections. What else does it need to qualify?

Totally agree pattern matching would be awesome though.

Collapse
 
nibelino profile image
Matteo

nice article! good job