-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already createdSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
Proposal
Typescript should be able to generate immutable objects with accessor properties and value semantics, similar to the "records" proposal for C# 7.0
Rationale
Currently I have to write a lot of boilerplate code to create an object with accessor properties and value semantics. I think this can easily be streamlined by the TypeScript compiler.
Example of what I have to write currently
class Person {
private _firstName: string;
private _lastName: string;
constructor(firstName: string = null, lastName: string = null) {
this._firstName = firstName;
this._lastName = lastName;
}
public get firstName(): string {
return this._firstName;
}
public get lastName(): string {
return this._lastName;
}
public equals(other: Person): boolean {
return other !== void 0
&& other !== null
&& this._firstName === other._firstName
&& this._lastName === other._lastName;
}
}
What I would like to write in future
class Person {
constructor(
public get firstName: string = null,
public get lastName: string = null) {
}
}
Note the use of get in the constructor
Either of the examples above should produce this (the top example did produce this)
var Person = (function () {
function Person(firstName, lastName) {
if (firstName === void 0) { firstName = null; }
if (lastName === void 0) { lastName = null; }
this._firstName = firstName;
this._lastName = lastName;
}
Object.defineProperty(Person.prototype, "firstName", {
get: function () {
return this._firstName;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Person.prototype, "lastName", {
get: function () {
return this._lastName;
},
enumerable: true,
configurable: true
});
Person.prototype.equals = function (other) {
return other !== void 0
&& other !== null
&& this._firstName === other._firstName
&& this._lastName === other._lastName;
};
return Person;
}());
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already createdSuggestionAn idea for TypeScriptAn idea for TypeScript