No, TypeScript is not OOP version of JavaScript
Tags: typescript, javascript
Published: 2020-03-05 15:02:06

The common misconception about TypeScript I hear a lot is - TypeScript is more OOP than JavaScript, TypeScript is more like Java, C#, was made 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](https://dev.to/educative/typescript-tutorial-a-step-by-step-guide-to-learn-typescript-4g4p). 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.

## TS has classes and inheritance so it's more OOP
TypeScript has classes, but JavaScript also has them. Take a look

```js
// JS🟨
class User {
    #name
    constructor(name) {
        this.#name = name;
    }
}
const user = new User('Tom');
```
The same part in TS
```ts
// TS🟦
class User {
    #name: string 
    constructor(name: string) {
        this.#name = name;
    }
}
const user = new User('Tom')
```
Any difference from additional type annotations? Don’t think so. Have you spotted private fields? Yes they are in both, as private fields went to [stage 3 of ECMAScript standard](https://github.com/tc39/proposal-class-fields).

### Inheritance
There for both, consider
```ts
// JS🟨 and TS🟦
class 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 don’t 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](https://en.wikipedia.org/wiki/Algebraic_data_type).

Instead of using `interface` and `extends`
```ts
interface X extends Y {x: number}
```
you can use `type` and intersection operator `&`
```ts
type X = Y & {x: number}
```
And you get the same result!

Check out below two equivalent versions of the same types definition
```ts
{
// 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 don’t need to use any interfaces in TS**, you can do everything by `type` and type operators like intersection and union. Yes really!

> **Note** Did you know that original idea of object oriented programming had nothing about inheritance. Alan Kay who had made this term was focusing on encapsulation and message passing.

## 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](https://ramdajs.com/), there are types for it. Also there are specially made libraries for TS like [fp-ts](https://github.com/gcanti/fp-ts), which represent pure statically functional languages type constructs and utilities. So yes, we can go fully functional!

> **Note** With newest feature of TS 4.0 - variadic tuple types, FP is slightly simpler to model!

## From where then the misconception came from? 

Probably there are few sources

- [TS creator](https://en.wikipedia.org/wiki/Anders_Hejlsberg) 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, don’t 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.