Closed
Description
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
Metadata
Metadata
Assignees
Labels
No labels