Skip to content

Object literal array type inconsistently inferred when using spread #30761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ssynix opened this issue Apr 4, 2019 · 2 comments
Closed

Object literal array type inconsistently inferred when using spread #30761

ssynix opened this issue Apr 4, 2019 · 2 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@ssynix
Copy link

ssynix commented Apr 4, 2019

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).

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Apr 10, 2019
@RyanCavanaugh
Copy link
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.

@ssynix
Copy link
Author

ssynix commented Apr 10, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants