-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript should have "Records" like C#7 #10910
Comments
The following is succinct and does most of what you are asking for: class Person {
constructor(
public readonly firstName: string = null,
public readonly lastName: string = null) {
Object.freeze(this); // this adds runtime immutability as well
}
} Since |
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. |
|
I do agree with @yortus, the intended scenarios should be covered by readonly properties. |
that too is the case in TS 2.0
it is really JS syntax/semantics. |
Perhaps also worth noting that if all you need is a simple record, classes are overkill. const p1 = { firstName: "Bart", lastName: "Simpson }; 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: interface Person {
readonly firstName: string;
readonly lastName: string;
} Then assign your object literals to that. To get freezing, write a simple constructor-like function: function person(firstName: string, lastName: string): Person {
return Object.freeze({ firstName, lastName });
}
const p = person("Homer", "Simpson");
p.firstName = "Marge"; // stopped at compile-type
(p as any).firstName = "Marge"; // stopped at runtime |
@yortus that is excellent. class Person {
constructor(
readonly firstName?: string,
readonly lastName?: string) {
Object.freeze(this); // this adds runtime immutability as well
}
} |
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) 🌹 |
@series0ne 'readonly' emits read-write property in JS. |
@nn Yes I know. For example:
Now it's really readonly! |
Actually, readonly can be useful for creating immutable classes if you return Object.freeze(this); 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. |
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
What I would like to write in future
Note the use of get in the constructor
Either of the examples above should produce this (the top example did produce this)
The text was updated successfully, but these errors were encountered: