It’s true that TypeScript does add these checks and regardless of whether or not you use functional programming, the move to TypeScript is well worth investing in it. But TypeScript and FP are not mutually exclusive.
To see how let’s look at function interfaces in TypeScript. Consider our
getName function from above. We might want to restrict the input to a User data object and extract the
name value. We could write this as
With a function interface this becomes
Of course this by itself seems even more verbose than the original. But for a whole class of similar methods it saves space while adding consistency. For example:
By revisiting our first composition we see we can use this interface yet again:
UserStringAttrFunc isn't the most concise name but you get the idea.
A common task is getting an existing type, mutating it (or a copy of it) in some way and returning the mutated type. This can be thought of as a
T → T transformation, an isomorphism of sorts.
In TypeScript we can describe this with mapped types:
With this mapped type we can now describe our composed mutator functions with ease.
One bit of icing on the cake for those using VS Code — hovering over functions defined with this mapped type wrapper will automatically fill in the specified type: