You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Spread operator does not call property getters #26547
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.
... 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.
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 Utility.Do should be flagged because obj cannot be guaranteed to implement the IWhatever interface. The programmer could still make the call by casting obj to IWhatever first.
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.
I don't think there's anything to do about it unless we were going to add a --strictOwnAndEnumerability flag to enforce tracking ownness/enumerability on all properties, which seems extraordinarily unlikely.
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
Reacted by Nimtar, Gabriel Samolis, Diego, FTWinston, Vova Kot and stan
functiongetModelProperties(model: any){returnObject.values(model)['0']asany}conststudent=newStudent()student.name="Chema"// supose you have the name setterconsole.log({data: { ... getModelProperties(student)}})// { data: { name: "Chema" }}
It seems the spread operator in Javascript skips over getters in classes, but not in objects.
Sometimes I feel the person implementing Javascript is really high.
class C { get x() { return 1; }}
const c = new C();
console.log({...c}); <---does not show x
const c = { get x() { return 1; }}
console.log({...c}); <---does show x
Activity
RyanCavanaugh commentedon Aug 20, 2018
TypeScript doesn't change the behavior of JavaScript.
andreashuber69 commentedon Aug 20, 2018
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...
... 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.
RyanCavanaugh commentedon Aug 20, 2018
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?
andreashuber69 commentedon Aug 20, 2018
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
Utility.Doshould be flagged becauseobjcannot be guaranteed to implement theIWhateverinterface. The programmer could still make the call by castingobjtoIWhateverfirst.andreashuber69 commentedon Aug 20, 2018
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.
RyanCavanaugh commentedon Aug 20, 2018
It wouldn't fix this case due to the indirection via
IWhateverandreashuber69 commentedon Aug 21, 2018
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?
RyanCavanaugh commentedon Aug 21, 2018
I don't think there's anything to do about it unless we were going to add a
--strictOwnAndEnumerabilityflag to enforce tracking ownness/enumerability on all properties, which seems extraordinarily unlikely.CHBaker commentedon Dec 14, 2018
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
ChemaCLi commentedon Apr 6, 2022
I made it in this way:
Brain2000 commentedon Jun 10, 2024
It seems the spread operator in Javascript skips over getters in classes, but not in objects.
Sometimes I feel the person implementing Javascript is really high.