DEV Community

Cover image for Easy functional programming techniques in TypeScript for everyone

Easy functional programming techniques in TypeScript for everyone

Deepu K Sasidharan on August 30, 2019

Originally published at deepu.tech. There is a lot of hype around functional programming(FP) and a lot of cool kids are doing it but it is not a s...
Collapse
 
aminnairi profile image
Amin

Hi there, great article! Did you know that you can use Object.freeze to prevent mutating an array or an object?

const list = Object.freeze([])

list.push(1)
// Throws an error
Collapse
 
macsikora profile image
Pragmatic Maciej

It will couse runtime exception. So Object.freeze is not useful in terms of TS.

Collapse
 
aminnairi profile image
Amin

Hi Maciej. You are absolutely right! That's why it is so good coupled with TypeScript which can prevent mutating a freezed object by refusing to transpile the file.

Thread Thread
 
macsikora profile image
Pragmatic Maciej

Still I see readonly as a tool for the job. Object.freeze is fully run-time thing and it is decoupled from type definition. Declaration of readonly is included into type definition, so it is explicit. I don't think run-time block is needed here.

Collapse
 
deepu105 profile image
Deepu K Sasidharan

Yes, I thought of writing about it but then missed

Collapse
 
stereobooster profile image
stereobooster • Edited

Unfortunately, there are not many ways to strictly limit data mutation in TypeScript

type Foo = {
  bar: number;
  bas: number;
}
type FooReadonly = Readonly<Foo>; 

or

const list = ["Apple", "Orange", "Banana", "Grape"] as const;

Mutation doesn't affect referential transparency - reference stays the same.

const test = {}
test.a = 1
test === test
Collapse
 
deepu105 profile image
Deepu K Sasidharan

I have updated the post

Collapse
 
deepu105 profile image
Deepu K Sasidharan

Yes, I missed that

Collapse
 
juliang profile image
Julian Garamendy

Hi! It may be worth mentioning that
"TypeScript comes with a ReadonlyArray type that is the same as Array with all mutating methods removed, so you can make sure you don’t change your arrays after creation"

typescriptlang.org/docs/handbook/i...

Collapse
 
deepu105 profile image
Deepu K Sasidharan

Oh yes, I completely forgot about those

Collapse
 
macsikora profile image
Pragmatic Maciej

functional data types such as Stacks, Maps and Queues.

Stack, Map and Queue are not data types related to FP, they have FP implementations, but still the sentence is wrong.

Collapse
 
deepu105 profile image
Deepu K Sasidharan

I have rephrased it

Collapse
 
deepu105 profile image
Deepu K Sasidharan

Sure they are not related to FP, what I meant was that they are more appropriate for the FP approach

Collapse
 
bobmyers profile image
Bob Myers

Virtually everywhere you say "TypeScript", you seem to mean "JavaScript". It is not TypeScript that treats functions as first-class objects, or lazily evaluates boolean expressions. It's JavaScript. TypeScript is merely JavaScript with type annotations.

Collapse
 
deepu105 profile image
Deepu K Sasidharan

Yes, and thats why I keep saying typeScript/JavaScript and also I made it clear in the beginning that TS is just a superset of JS

Collapse
 
jmfayard profile image
Jean-Michel 🕵🏻‍♂️ Fayard

What kind of markdown do you use to link to the other articles at the top? It looks neat!

Collapse
 
deepu105 profile image
Deepu K Sasidharan

Its the series feature in Dev, if you add series: my series name to the front matter of the post-it links them up that way