Hacker News new | ask | show | jobs
by cryptica 2559 days ago
Also I think part of the problem is that TypeScript encourages developers to write interfaces that have complex parameters (e.g. accept instances of specific classes) whereas JavaScript encourages simpler parameters like strings, numbers or plain JSON.

In OOP, passing complex instances to functions across different files is dangerous because the different files may all end up affecting the same instance's internal state and you don't know which file is responsible for what change (and it can lead to conflicting states/bugs). The absence of types in JavaScript tends to discourage this design precisely because it makes it difficult to express those kinds of complex/rigid class relationships.

JS encourages developers to keep each kind of "live" instance in a single file and only return raw data from that file. For me, restricting all instance mutations to one file is the secret to writing good OOP code which has clear separation of concerns.

1 comments

TypeScript doesn't encourage returning classes. Writing classes just as data handlers is still verbose and clumsy. You can have a team that's writing Java in TypeScript and see that, but that's not conventional at all.

Instead, TypeScript encourages returning structurally typed interfaces. Which are just data.