-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Spread operator does not call property getters #26547
Comments
That's a fair point, ts isn't responsible for how js works. However, if we expand the example code just a little bit, e.g. as follows... interface IWhatever {
readonly value: number;
}
class Whatever implements IWhatever {
public get value() {
return 42;
}
}
class Utility {
public static Do(x: IWhatever) {
return x.value;
}
}
const whatever: IWhatever = new Whatever();
const obj = { ...whatever, otherValue: 43 };
console.log(Utility.Do(obj)); ... it becomes clear that the compiler does assume that obj implements the interface, otherwise the code should not compile. More precisely, the compiler falsely assumes that obj will always have a value property even though there's the possibility that it might not. |
See also #26287 I'm not sure what you're proposing happen. We don't track own/enumerability (see other open issues on that) so what line should be an error? |
I'm not sure what should happen, I just think this is a hole in the type system. If I had to implement this, I'd say the compiler should not make any assumptions what properties are added to an object by the spread operator. So, the call to |
And yes, #26279 is very related. The issue only talks about classes with getters, so I'm not sure whether the fix would also correct the issue WRT interfaces as shown above. |
It wouldn't fix this case due to the indirection via |
Ok, thanks. How are we going forward with this problem (if it is one)? Should I file another issue that more succinctly explains the matter? |
I don't think there's anything to do about it unless we were going to add a |
Would it be possible to type cast the spread operator value with the properties it is filling in? I think that would be useful instead of assuming the coder is correct |
I made it in this way: function getModelProperties (model: any) {
return Object.values(model)['0'] as any
}
const student = new Student()
student.name = "Chema" // supose you have the name setter
console.log({ data: { ... getModelProperties(student)} })
// { data: { name: "Chema" } } |
It seems the spread operator in Javascript skips over getters in classes, but not in objects.
|
Search Terms:
spread operator, property getter
Code
Expected behavior:
{ value: 42, otherValue: 43 }
This is expected because a readonly property of an interface can be implemented with a getter. The code using the interface cannot know that the property is implemented with a getter and therefore expects to see the property of the interface in the result.
Actual behavior:
{ otherValue: 43 }
Playground Link:
Link
Related Issues:
The text was updated successfully, but these errors were encountered: