Skip to content

Unable to narrow union to Record in TS 4.8Β #50671

Closed
@AlCalzone

Description

@AlCalzone

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

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"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions