Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript classes vs typescript classes

I am new to TypeScript and JavaScript classes!

I was learning TypeScript where I created something as simple as this

class User {
  name: string;
  email: string;

  constructor(name: string, email: string) {
    this.name = name;
    this.email = email;
  }
}

let newUser = new User("Rohit Bhatia", "[email protected]");

and this was given to me as equivalence

var User = /** @class */ (function () {
    function User(name, email) {
        this.name = name;
        this.email = email;
    }
    return User;
}());
var newUser = new User("Rohit Bhatia", "[email protected]");

Now, I have three questions

  1. what is @class (or @ in general in JavaScript)? var User = /** @class */ (function () {

  2. classes are in JavaScript as well? So why doesn't TypeScript transform them into JS classes?

  3. in TS class we can do something like this

    class User { name: string; email: string;

but can't we do something like this in JavaScript? Or what is the difference between JS classes and TS classes?

like image 265
anny123 Avatar asked Feb 23 '19 12:02

anny123


People also ask

Should I use classes with TypeScript?

When should we use classes and interfaces? If you want to create and pass a type-checked class object, you should use TypeScript classes. If you need to work without creating an object, an interface is best for you.

Is TypeScript really better than JavaScript?

TypeScript vs JavaScript: HighlightsJavaScript is better suited for small-scale applications, while TypeScript is better for larger applications. TypeScript supports static typing but JavaScript does not. TypeScript supports interfaces but JavaScript does not. TypeScript features prototyping but JavaScript does not.

Can I use TypeScript class in JavaScript?

You can have typescript compile javascript files as well using 'allowJs' (in tsconfig. json). That way, you can reference typescript classes from your javascript.

What is the main difference between JavaScript and TypeScript?

TypeScript is an object-oriented programming language developed by Microsoft Corporation, whereas JavaScript is the programming language for the web. TypeScript is an open-source language to build large-scale web apps, whereas JavaScript is a server-side programming language that helps to develop interactive web pages.


2 Answers

Answering your questions:

  1. @class is a kind of annotation/comment that nothing as to do with the standard.

  2. In ES5 (let's say "classic JavaScript") there are no classes, but there is a way to simulate their behaviour, also when TypeScript code is transpiled to ES5. That way to code "classes" (remember that they aren't there) is a bit harder compared to new specifications.

  3. See answer 2 too. Also:

Since the modern JavaScript ECMAScript 6 specification (ES6), now JavaScript has classes. TypeScript is a kind of evolution of ES6. In ES6, it would be like this:

class User {
  constructor(name, email) {
    this.name = name;
    this.email = email;
  }
}

var newUser = new User('Rohit Bhatia', '[email protected]');
like image 137
José Antonio Postigo Avatar answered Sep 16 '22 22:09

José Antonio Postigo


  1. /** @class */ is just a comment
  2. TypeScript's default target is ES5 so it transpiles to JS code that can execute on browsers as old as IE11. If you set ES6 as target, you'll notice that JS classes will be used
  3. ES5 way for writing classes is to use a function as a constructor, but the result when executing the code is exactly the same as ES6 classes
like image 32
Guerric P Avatar answered Sep 18 '22 22:09

Guerric P