You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
}
**Expectedbehavior:**Iexpecttonotfeellikeanidiot(nothingyoucanbugfix),butshouldn't this just... work? I'mpassinganobject,I've type guarded that it is an object... and being told I can'tuseitasit's not an object. In the vague hopes that I'mnotjustbeingadummy,I've posted this here because it may be a bug? I dunno, best of luck.
**Actualbehavior:**TSCcompilererror
The text was updated successfully, but these errors were encountered:
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.
For example, the following does not have a compiler error however loses all type information of 'base'.
The text was updated successfully, but these errors were encountered: