Skip to content

Object literal array type inconsistently inferred when using spread #30761

Closed

Description

@ssynix

TypeScript Version: 3.3.3333

Search Terms:

  • object literal array type
  • object array property does not exist
  • spread object literal array

Code

Playground: http://tinyurl.com/y65n9b3o

const array1 = [
    { a: 'a', b: 'b' },
    { a: 'a', c: 'c' },
] // inferred: Partial<{ a, b, c }>[]

array1.map(({ c }) => c); // works ✅


const array2 = [
    { a: 'a', b: 'b' },
    { a: 'a', ...{ c: 'c' } },
]

array2.map(({ c }) => c); // suddenly error? ❌

Expected behavior:
I'm not sure if this is a bug but I expect both map calls to type-check identically. If it's not a bug, it's very unintuitive why the spread syntax completely changes how the array type is inferred. The only workaround I have is to explicitly write the full type (which isn't as concise as the example above).

Activity

  1. RyanCavanaugh commented on Apr 10, 2019

    @RyanCavanaugh
    Member

    We have "special" logic for unifying object literals in these cases; the spread syntax interferes with that.

    If you post a more representative sample where it's not just spreading for the sake of spreading, we may be able to advise more.

  2. ssynix commented on Apr 10, 2019

    @ssynix
    Author

    Here's almost the same code that reproduces the error.

    Is the special logic documented somewhere? It would help avoid surprises and confusion if the logic is more explicitly defined, or if there are ways to control the inference.

  3. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions