16

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?

1
  • 4
    Classes weren't added to JS until the ES2015 version of the spec - the code outputted by the TypeScript compiler is functionally identical to a JS class (as JS classes are just syntax sugar for functions), but will work on browsers that do not support them. If you set the TypeScript compiler to output ES2015 code via the target option, I believe the class will be output as a proper JS class. Commented Feb 23, 2019 at 12:12

3 Answers 3

6

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]');
Sign up to request clarification or add additional context in comments.

Comments

6
  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

2 Comments

So,conclusion,classes in ts === classes in js?
Yes, or constructor function if ES5 is set as transpilation target
1

I'm answering to complete the third question.

  1. In ECMAScript standard, what is between /* and */ is just a comment, but it's possible that TypeScript transpiler or debugger use this annotations for something more.
  2. As said in the other answers, by default TypeScript transpile for ECMAScript 5 (ES5), for better compatibility with old browsers, and that it's a way to do something like class in ES5.
  3. The ES6 classes are similar to TypeScript classes, but TypeScript also has public/protected/private modifiers, that there aren't in ES6 classes. Also, in TypeScript you will have advantages by the stronger typing, which includes even better autocomplete in some editors (like Visual Studio Code, of course it depends of the editor).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.