Functional programming in TypeScript
In this workshop, we will learn the power of types in TypeScript, and then use that power responsibly to create our own strongly typed functional library.
Part 0 - Prerequisites
- Visual Studio Code
- When prompted, install recommended extensions
- Run
npm i
Part 1 - Native functions
EcmaScript 5 and newer versions already have quite a few functional operators as part of the language.
These are all automatically typed as part of the TypeScript built-in typings; for example, try the following:
const values = [1, 2, 3]
const strings = values.map(x => 'string' * 1)
What is the type of strings?
That's right, string[]
!
To explore the typings of these, Ctrl+Click .map
, and you will see the content of node_modules/typescript/lib/lib.es5.d.ts
. This file and others in the same folder are the built-in typings of TypeScript, and can be used as a reference for best practice when writing your own types.
Part 2a - Write your own functional library
Do this part OR part 2b.
- Open
fs-lib.ts
andfs-lib.spec.ts
npm run test:fs-lib
- Implement till test works
- Extend with your own favourite functional functions
Part 2b - Write typings for Lodash
- Open
lodash-es.d.ts
andlodash-es.spec.ts
npm run test:lodash
- Write typings till they are as expected
- Extend with your own favourite lodash functions
Cheats:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/lodash/fp.d.ts https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/lodash/common
fp-ts
Part 3 - Check out https://gcanti.github.io/fp-ts/
Follow this tutorial series: https://dev.to/gcanti/getting-started-with-fp-ts-setoid-39f3
Cool stuff:
GitHub issue tracking full monad support in TypeScript Article about FP in JS Maybe in TypeScript