Skip to content

Type narrowing based on function typeof is broken when function is intersected with a recordΒ #45801

Closed
@safareli

Description

@safareli

Bug Report

πŸ”Ž Search Terms

type narrowing based on function typeof is broken when intersection with a record is used

πŸ•— Version & Regression Information

  • Recent change in behaviour happened in version 4.3.5

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Meta = { foo: string }
interface F { (): string}
const x = (a: (F & Meta) | string) => {
    if (typeof a === "function"){
        // ts.version >= 4.3.5: never -- unexpected
        // ts.version <= 4.2.3: F & Meta -- expected
        a;
    } else {
        // expected: string
        // ts.version >= 4.3.5: string -- expected
        // ts.version <= 4.2.3: string | (F & Meta) -- unexpected
        a;
    }
}

πŸ™ Actual behavior

behavior was half correct from in TS versions <= 4.2.3, but then it was changed in 4.3.5 which fixed the part that was wrong previously, but broke the other part that was correct. So we still have half broken behavior.

πŸ™‚ Expected behavior

It's expected to pick the correct parts from the both versions.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions