question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion: perform excess property checks when spreading an inline object literal

See original GitHub issue

TypeScript 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:open
  • Created 3 years ago
  • Reactions:32
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

17reactions
maksimfcommented, Apr 24, 2021

Is there any update on this? This is really annoying that ts doesn’t check that

8reactions
jamesa3commented, May 24, 2022

Any update on this?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found