Closed
Description
Bug Report
π Search Terms
union record narrow type guard
π Version & Regression Information
- This changed between versions 4.7.4 and 4.8.2
β― Playground Link
π» Code
function isObject<T>(it: T): it is T & Record<string, unknown> {
return Object.prototype.toString.call(it) === "[object Object]";
}
declare function test(arg: Record<string, any>): void
declare const arg: (string | number | { a: "b" });
if (isObject(arg)) {
// arg is:
// - TS 4.7: {a: "b"}
// - TS 4.8: (string | number | { a: "b" }) & Record<string, unknown>
test({ ...arg }) // ERROR: Spread types may only be created from object types.(2698)
}
π Actual behavior
arg
stays as some intersection type, which TS does not recognize as an object type.
π Expected behavior
arg
gets narrowed to {a: "b"}