Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 5, 2020 03:02 pm GMT

No, TypeScript is not OOP version of JavaScript

The common misconception about TypeScript I hear a lot is - TypeScript is more OOP than JavaScript, TypeScript is more like Java, C#, its for OOP programmers, it emphasizes classes and inheritance.

One of the last examples of such misconception being shared was part of quite popular article - TypeScript Tutorial: A step-by-step guide to learn TypeScript. Below quote from the article

TypesScript is an Object oriented programming language whereas JavaScript is a scripting language

Such a false statement in one of the most popular articles in dev.to about TypeScript . I decided to try to fight with these statements, and show you that TS is not in any bit more object oriented than JS itself.

Classes

TypeScript has classes, but JavaScript also has them. Take a look

// JSclass User {    #name    constructor(name) {        this.#name = name;    }}const user = new User('Tom');

The same part in TS

// TSclass User {    #name: string     constructor(name: string) {        this.#name = name;    }}const user = new User('Tom')

Any difference from additional type annotations? Dont think so. Have you spotted private fields? Yes they are in both, as private fields went to stage 3 of ECMAScript standard.

Inheritance

There for both, consider

// JS and TSclass Admin extends User{    #type = 'admin'}const admin = new Admin('Tom');

What is TS version of the above? The same yes thanks to type inference I dont need to change a bit. Where is the difference then? There is none

TypeScript type system emphasize OOP

That is true that TS has concept of interface, concept familiar for people working with statically typed object oriented languages like Java or C#. But TypeScript also has alternative syntax for types, and it's typical for functional languages, it is based on algebraic data types.

Check out below two equivalent versions of the same types definition

{// interface version - familiar for Java/C#    interface User {        type: string        name: string    }    interface Admin extends User {        type: 'admin'    }    interface Moderator extends User {        type: 'mod'    }    function test(u: User) {        return u;    }    const admin: Admin = {        type: 'admin',        name: 'Tom'    }    test(admin) // admin can be used as user}{// algebraic types version - familiar for functional programmers Haskell, Elm    type User = {        type: string        name: string    }    type Admin = User & {        type: 'admin'    }    type Moderator = User & {        type: 'mod'    }    function test(u: User) {        return u;    }    const admin: Admin = {        type: 'admin',        name: 'Tom'    }    test(admin) // admin can be used as user}

You dont need to use any interfaces in TS, you can do everything by type and type operators like intersection and union. Yes really!

Functional programming in TypeScript is hard

This last one is outside of the original argument, but if not OOP, then natural choice is Functional programming, so if we resign from classes we will most probably write functions and data transformations in the functional style.

Is then functional programming harder in TS than in JS? Yes it is slightly, but fortunately there are tools for that, most famous functional libraries for JS are fully typed, so if you use for example Ramda, there are types for it. Also there are specially made libraries for TS like fp-ts, which represent pure statically functional languages type constructs and utilities. So yes, we can go fully functional!

From where then the misconception came from?

Probably there are few sources

  • TS creator is the same person who made C#
  • TS had classes and private fields before JS
  • Type notation (interfaces, generics) looks like in Java, C#

First is rather wrong, as a person who made one language no needs to make other languages the same. Second is only historically true, but we have 2020, dont we? Third is only about grammar, yes looks similar but it has nothing if the language is object oriented or not.

And to be clear TypeScript is object oriented language, but object orientation is not in any way better than in JavaScript itself. Historically that was true, as TS introduced classes, private fields and inheritance when there was no such equivalent in JS. But now it is not true, everything related to object orientation exists in JS, TypeScript only adds type level to it.

TypeScript is JavaScript with additional static types level, both are multi-paradigm programming languages. Next time if somebody will tell you TS is only for OOP, tell him that it's nothing more than a lie.


Original Link: https://dev.to/macsikora/no-typescript-is-not-oop-version-of-javascript-3ed4

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To