So I don't think that stating that TypeScript favours class-based object-orientation a lot more than JavaScript is too far off the mark. Functional-style TypeScript is often reminiscent of that C# mess.
#YOU DONT KNOW JS PROTOTYPAL INHERITANCE CODE#
In Dependency Injection revisited (2018) ( Blog post) Mark Seeman juxtaposes F# code against the somewhat equivalent C# code which looks like a verbose mess. Once we accept that in TypeScript the class-based object-oriented style is the path of least resistance (according to: "make the right thing easy and the wrong thing hard") I think it is fair to say that TypeScript skews the choice of implementation towards class-based object-orientation much more than JavaScript does. Functional style TypeScript requires a much better grasp of TypeScript's typing meta-language - to a degree that isn't usually necessary when practicing a class-based object-oriented style (example: Learn Advanced TypeScript). I would also observe that slightly is an understatement. In my view this statement undermines the title. Is then functional programming harder in TS than in JS? Yes it is slightly. No, TypeScript is not OOP version of JavaScript Like comment: Like comment: 6 likes Thread Each much more important than achieving the perfect level of encapsulation imo. Nice hack to use WeakMap, but to me it's a waste of effort, increased maintenance hurdle/cost, cognitive load, and maybe even performance.
![you dont know js prototypal inheritance you dont know js prototypal inheritance](https://miro.medium.com/max/6436/1*7UAKs86_jTQGWe-Ieqr2qQ.jpeg)
Sure # just makes it more convenient, so that's nice, but definitely not a requirement to me. Again it communicates that this variable should be considered as private state to the said class. Python solves that by prefixing the variable name with _ClassName_, transparently for you. The only imo minor inconvenience of private compared to # is collision. Private works just fine to achieve exactly that and other modifiers like readonly etc. That the right way is most convenient and well surfaced, and the wrong way is harder.
![you dont know js prototypal inheritance you dont know js prototypal inheritance](https://miro.medium.com/max/1104/1*6t_3iCBsYq5x11E_Lr-V8w.png)
In the end, it's important that your code communicates how it should be used, If you want to break a program, you can break it one way or another, so why try to chase the perfect level of encapsulation? Most languages who support encapsulation better than JS, still allow access through reflection. I don't understand why you would do this. * class Todo Like comment: Like comment: 1 like Thread
![you dont know js prototypal inheritance you dont know js prototypal inheritance](https://pbs.twimg.com/media/EmE8SGGXEAItAuD.jpg)
Next time if somebody will tell you TS is only for OOP, tell him that it's nothing more than a lie. TypeScript is JavaScript with additional static types level, both are multi-paradigm programming languages. But now it is not true, everything related to object orientation exists in JS, TypeScript only adds type level to it. Historically that was true, as TS introduced classes, private fields and inheritance when there was no such equivalent in JS. Second is only historically true, but we have 2020, don’t we? Third is only about grammar, yes looks similar but it has nothing if the language is object oriented or not.Īnd to be clear TypeScript is object oriented language, but object orientation is not in any way better than in JavaScript itself.
![you dont know js prototypal inheritance you dont know js prototypal inheritance](https://miro.medium.com/max/1280/1*TMlI4f5m3C4rFm61WO5uWg.png)