为什么 TSC 说“...base”必须是一个对象,我该如何解决这个问题,同时仍然保留“base”对象的类型。
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
}
例如,以下没有编译器错误,但是丢失了“base”的所有类型信息。
function aFunction(base: object) {
const anObject = { test:value }
if (typeof base !== 'object') { return }
const merged = { ...base, anObject }
return merged
}