Repository navigation
TypeScript should have "Records" like C#7 #10910
Copy link
Copy link
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;
}());
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already createdSuggestionAn idea for TypeScriptAn idea for TypeScript
Activity
yortus commentedon Sep 14, 2016
The following is succinct and does most of what you are asking for:
Since
equalsis no sort of standard method in JavaScript, I can't imagine that part being implemented by the TS compiler.MrMatthewLayton commentedon Sep 14, 2016
Troy Gerwien (@yortus)
Emiting ES5 accessor properties with only a getter would naturally create immutable properties...swings and roundabouts as to whether emitting getters or using Object.freeze would be better.
In general I just thing the whole property syntax in TypeScript sucks.
yortus commentedon Sep 14, 2016
readonlyis a 2.0 feature. Read about it in #6532. It's purely for static type-checking and doesn't affect the emit, so the JavaScript output contains ordinary mutable fields.mhegazy commentedon Sep 14, 2016
I do agree with Troy Gerwien (@yortus), the intended scenarios should be covered by readonly properties.
mhegazy commentedon Sep 14, 2016
that too is the case in TS 2.0
it is really JS syntax/semantics.
danielearwicker commentedon Sep 17, 2016
Perhaps also worth noting that if all you need is a simple record, classes are overkill.
And we're done. If you want to centrally enforce the type that a person conforms to, e.g. to stop writing to the properties:
Then assign your object literals to that. To get freezing, write a simple constructor-like function:
aluanhaddad commentedon Sep 18, 2016
Troy Gerwien (@yortus) that is excellent.
I would like to add that it can be made even more concise and idiomatic
basarat commentedon Sep 18, 2016
Also for record equality you can use this function : https://www.npmjs.com/package/deep-equal when feeling lazy (like I've done a few times e.g in alm) 🌹
NN--- commentedon May 20, 2017
@series0ne 'readonly' emits read-write property in JS.
It is not the same as getter-only property.
Unfortunately there is still no shorthand for getter-only property.
MrMatthewLayton commentedon May 22, 2017
nn (@nn) Yes I know.
readonlyis rather useful (provided one does not play with the emitted JS). I also find it quite useful to usereadonlyin conjuction withObject.freeze(...)For example:
Now it's really readonly!
aluanhaddad commentedon May 22, 2017
Actually, readonly can be useful for creating immutable classes if you
from the constructor. The advantage over using accessors is that you can leverage inline initialization, parameter properties, and the resulting object is spreadable.
All in all though, I much prefer factories to classes. Especially with the addition of Object Rest/Spread, object literals are better than ever.