TypeScript Doesn’t Turn JavaScript Into a Class-Based OOP Language
TypeScript is just a natural extension of JavaScript. It doesn’t turn it into a new language. Rather, it just adds type annotations and checking to exist JavaScript code.
In this article, we’ll look at why TypeScript isn’t a class-based OOP language.
TypeScript Classes Aren’t Classes
A TypeScript class is just a JavaScript class with some data type annotations and generic data types markers added to it.
However, those are just add-ons to a JavaScript class, which is just syntactic sugar on top of constructor functions.
For instance, if we have the following JavaScript class:
class Foo {
name: string;
constructor(name: string) {
this.name = name;
}
}
We see that when we log the type of the Foo
class as follows:
console.log(typeof Foo)
We see that it’s a function. It’s no different from the regular JavaScript class, which is just a constructor function.
Underneath, the constructor is just assigning items to this
from the function parameters. And instance methods are just properties of the prototype of the function.