Suggestion: perform excess property checks when spreading an inline object literal
See original GitHub issueTypeScript Version: 3.9.2
Search Terms:
Code
declare const someCondition: boolean;
type MyObject = { foo: number; bar?: number };
const a: MyObject = {
foo: 1,
bar: 2,
// ✅ Error because `invalid` is an excess property
invalid: 3,
};
const b: MyObject = {
foo: 1,
...(someCondition
? {
bar: 2,
// ❌ `invalid` is an excess property, but we don't get an error here
invalid: 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.
Expected behavior:
An error
Actual behavior:
No error
Playground Link:
Related Issues:
Issue Analytics
- State:
- Created 3 years ago
- Reactions:32
- Comments:7 (1 by maintainers)
Top Results From Across the Web
TypeScript — Excess Property Checks | by Tal Ohana
When TypeScript encounters object literal in assignments or when passed as an argument to functions it triggers an action called excess property ......
Read more >Understanding Excess Property Checking in Typescript
Excess property checking is when Typescript checks your object to ensure that it doesn't contain any extra properties on top of what is...
Read more >Objects • JavaScript for impatient programmers (ES2022 edition)
Object literals are one way of creating fixed-layout objects. They are a stand-out feature of JavaScript: we can directly create objects – no...
Read more >Announcing TypeScript 4.4 Beta - Microsoft Developer Blogs
One use of this might be to exempt properties starting with data- from TypeScript's excess property checking. When we pass an object literal...
Read more >Overview - TypeScript
Improved excess property checks in union types ... In TypeScript 3.2, object literals now allow generic spread expressions which now produce intersection ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Is there any update on this? This is really annoying that ts doesn’t check that
Any update on this?