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
what is
@class
(or@
in general in JavaScript)?var User = /** @class */ (function () {
classes are in JavaScript as well? So why doesn't TypeScript transform them into JS classes?
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?
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
what is
@class
(or@
in general in JavaScript)?var User = /** @class */ (function () {
classes are in JavaScript as well? So why doesn't TypeScript transform them into JS classes?
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?
Share Improve this question edited Feb 19, 2022 at 0:42 Paolo 21.1k21 gold badges76 silver badges121 bronze badges asked Feb 23, 2019 at 12:06 AlwaysblueAlwaysblue 11.8k44 gold badges139 silver badges252 bronze badges 1 |3 Answers
Reset to default 6Answering your questions:
@class
is a kind of annotation/comment that nothing as to do with the standard.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.
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]');
/** @class */
is just a comment- 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
- 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
I'm answering to complete the third question.
- 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. - 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.
- 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).
target
option, I believe the class will be output as a proper JS class. – Joe Clay Commented Feb 23, 2019 at 12:12