Skip to content

Error attempting to merge objects using spread operator #17507

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
laramiestreams opened this issue Jul 29, 2017 · 2 comments
Closed

Error attempting to merge objects using spread operator #17507

laramiestreams opened this issue Jul 29, 2017 · 2 comments

Comments

@laramiestreams
Copy link

TypeScript Version: 2.4.2

Code
Why does TSC say that "...base" must be an object, how do I resolve this problem while still retaining the types from the "base" object.

function aFunction<T extends object>(base: T) {
  const anObject = { test:"value" }
  if (typeof base !== 'object') { return }

// the following line causes a TSC error, saying that spread types can only be
// created from object types and highlighting base as the problem...  wut?

  const merged = { ...base, anObject }
  return merged
}

For example, the following does not have a compiler error however loses all type information of 'base'.

function aFunction(base: object) {
  const anObject = { test:value }
  if (typeof base !== 'object') { return }

  const merged = { ...base, anObject }
  return merged
}
**Expected behavior:**
I expect to not feel like an idiot (nothing you can bugfix), but shouldn't this just... work?  I'm passing an object, I've type guarded that it is an object... and being told I can't use it as it's not an object.  In the vague hopes that I'm not just being a dummy, I've posted this here because it may be a bug?  I dunno, best of luck.

**Actual behavior:**
TSC compiler error
@aluanhaddad
Copy link
Contributor

Duplicate of #13557

@laramiestreams
Copy link
Author

woops

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants