No, TypeScript is not OOP version of JavaScript
Pragmatic Maciej
Posted on March 5, 2020
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. 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🟨
class User {
#name
constructor(name) {
this.#name = name;
}
}
const user = new User('Tom');
The same part in 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.
Inheritance
There for both, consider
// 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.
Instead of using interface
and extends
interface X extends Y {x: number}
you can use type
and intersection operator &
type X = Y & {x: number}
And you get the same result!
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 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, 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!
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 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.
Posted on March 5, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 28, 2024