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
Suggestion: perform excess property checks when spreading an inline object literal #39998
declareconstsomeCondition: boolean;typeMyObject={foo: number;bar?: number};consta: MyObject={foo: 1,bar: 2,// ✅ Error because `invalid` is an excess propertyinvalid: 3,};constb: MyObject={foo: 1,
...(someCondition
? {bar: 2,// ❌ `invalid` is an excess property, but we don't get an error hereinvalid: 3,}
: {}),};
In the example above, I only want to include specific properties when a condition is met. That's the only reason I'm using spread here.
I understand that TypeScript only performs excess property checks inside of object literals. Currently this does not include inline object literals which are being spread inside of another object literal.
In my experience this is a very common code pattern so it would be great if TypeScript handled this.
Reacted by Florian Spier, Andrei Mukhin, Rare Kang, David Caetano, Yoav Balasiano, Georgii Korshunov, jonapgartwohat, Egor Andreyuk, Hollis Lau, Jake Leventhal and 7 more
We've been caught by surprise a few times by this in production; specifically in cases where we renamed an optional property as part of some refactor. To be fair, unit tests could've caught the issues too; we were lacking some coverage. But this behavior can be a little surprising, even for those that have been using TypeScript for a while. Probably because it feels inconsistent with how excess property checking works for object literals in general.
Reacted by Peter Flynn, Miguel Ángel and Vincent Jaubert
Any update on this? In 2023 we rely too much on a tool which has such a big hole in type security. I can use 'strict' flag but I still loose types due to spreading my objects.
A very much needed fix... Spreading objects is a really common case
Reacted by Leonid Pyrlia, devmargooo, pc-erin, Aleks-Daniel Jakimenko-Aleksejev, iizyd, Sebastian Fredriksson Bernholtz, Artjoms Faltins, puri0504, zhang and Andrei Don
Reacted by Roger Schönbächler, Sebastian Fredriksson Bernholtz, Anton, Alex C, Oleksandr Boiko, Josh Duff, Boris Ghidaglia, vincent-guyon_Atos, Victor Sheyanov, Peter Flynn and 3 more
I recently encountered this issue and was surprised to find that TypeScript ignores the error. It's astonishing to see that this problem has been around since at least 2017 without being resolved. It's quite disappointing.
Reacted by harish-github35, Aris-Arsen Kashari, Andreas Hippler, Miguel Ángel, zhang, maksimrk, Nick Ribal, Sergey Shevchenko, Taylor Edwards and zacaj
Hi!
I'm totally disappointed too because of this illogical behavior. I've got the problem with a simple case too, similar to the one of devmargooo above.
It is sad that Typescript can't detect this problem. We have to wait the execution of the software and the class-validator validation of the object to detect the error, whereas TypeScript could have done it as early as during coding in the IDE and during the build. :'(
If TS can't validate satisfies when a spread is used, at the very least it should error and not allow spreads to be used- this is silently ignoring the whole point of using satisfies as opposed to other type assertions
Activity
ifeltsweet commentedon Aug 21, 2020
Absolutely agree with the suggestion here. We do stuff like this all the time:
OliverJAsh commentedon Feb 22, 2021
Just realised, this issue isn't just about excess property checks. We're also missing errors for non-excess properties (#41698):
maksimf commentedon Apr 24, 2021
Is there any update on this? This is really annoying that ts doesn't check that
dwjohnston commentedon Aug 9, 2021
Note that this issue isn't just on spreading immediately declared object literals, spreading a function parameter can introduce this issue:
gerasimvol commentedon Apr 12, 2022
same bug
jamesa3 commentedon May 24, 2022
Any update on this?
fix(hooks-web): don't pass widget props to ui components
fix(hooks-web): don't pass widget props to ui components (#3501)
bdpartridge commentedon Nov 23, 2022
We've been caught by surprise a few times by this in production; specifically in cases where we renamed an optional property as part of some refactor. To be fair, unit tests could've caught the issues too; we were lacking some coverage. But this behavior can be a little surprising, even for those that have been using TypeScript for a while. Probably because it feels inconsistent with how excess property checking works for object literals in general.
fix(hooks-web): don't pass widget props to ui components (algolia/rea…
RCnowak commentedon Jan 23, 2023
Any update on this? In 2023 we rely too much on a tool which has such a big hole in type security. I can use 'strict' flag but I still loose types due to spreading my objects.
juanrgm commentedon Feb 24, 2023
Same case here and very surprised 😕.
https://www.typescriptlang.org/play?#code/MYewdgzgLgBCBGArApsKBGAXDA3gWACgZiZkwBDeAG2QBNt4QQbyxCBfGAXl0JNIrU62KACcArsgA0fEgEswAN3JU59GGMlSYAeh2lRokKNnEAdBfxF+JVgE8oACwUBzORGWr1m5Lv1gQAyMTa2J2DkJCUEhYBBQ0ACZuXlCBShpvCWlTGAVPNREs7T0g4xyLMysbWzAHZzA3DxUCjSy-GADSkP5wgk4Icih3ADM5ZAgU-jJ04RhGZmRWDiA
thepuzzlemaster commentedon Mar 3, 2023
I came across this unexpectedly as well when trying to conditionally add some properties to a return object.
https://stackoverflow.com/questions/75621849/why-does-using-spread-synax-not-honor-type-safety-for-an-expected-return-ty/75622021#75622021
Kasik-D commentedon May 14, 2023
I wrote type to check is T type compare to K type, but I don't know how this code improve and transform to use on function
Playground
Harpush commentedon Jun 7, 2023
A very much needed fix... Spreading objects is a really common case
devmargooo commentedon Jun 15, 2023
Same bug even in very simple case
https://www.typescriptlang.org/play?ssl=10&ssc=37&pln=1&pc=1#code/C4TwDgpgBAYg9nKBeKBvAUFLUBmCBcUAzsAE4CWAdgOYDc6AvuuqJFAEICGpyam2AI26ESFGvSboAxnEokoQ0vi48UqBcKgAiLVAb0Zc4LgLxEaqADprigDQm4hHXtpA
megaboich commentedon Mar 28, 2024
I recently encountered this issue and was surprised to find that TypeScript ignores the error. It's astonishing to see that this problem has been around since at least 2017 without being resolved. It's quite disappointing.
vtgn commentedon May 14, 2024
Hi!
I'm totally disappointed too because of this illogical behavior. I've got the problem with a simple case too, similar to the one of devmargooo above.
It is sad that Typescript can't detect this problem. We have to wait the execution of the software and the class-validator validation of the object to detect the error, whereas TypeScript could have done it as early as during coding in the IDE and during the build. :'(
dpeter99 commentedon Aug 26, 2025
I have just run into this at work, while trying to figure out whay I wasn't getting "may only specify known properties" warnings.
zacaj commentedon Jan 9, 2026
If TS can't validate
satisfieswhen a spread is used, at the very least it should error and not allow spreads to be used- this is silently ignoring the whole point of usingsatisfiesas opposed to other type assertionsOliverJAsh commentedon Feb 18, 2026
It's worth mentioning the need for spreading arises when using
exactOptionalPropertyTypes.Without
exactOptionalPropertyTypes, you don't need to use spread (ref):With
exactOptionalPropertyTypes, you need to use spread (ref):aweebit commentedon Aug 21, 2026
This would be really, really useful for React / JSX as simplifying code like this
by deduplicating prop / variable names like this
would no longer mean that no excess property checking would be done.
A currently available workaround is to add a
satisfiesannotation:But it's very clumsy and makes the code confusing for no reason.
aweebit commentedon Aug 21, 2026
The problem with conditionally added properties also affects JSX:
An error should obviously be reported here.