Skip to content

Type narrowing of union types by type predicate narrows to only first branch of unionΒ #44754

@Pajn

Description

@Pajn

Bug Report

πŸ”Ž Search Terms

union type, type predicate

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "Common Bugs That Aren't Bugs"

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Foo = {kind: 'foo'}
interface Bar<T = Any> {kind: 'bar', value: T}
type Baz = {kind: 'baz'}
type Any = Foo | Bar | Baz

function isFoobar(value: unknown): value is Foo | Bar<Foo> {
    return typeof value === 'object' && ((value as any)?.kind === 'foo' || (value as any)?.kind === 'bar')
}

let value = {kind: 'baz'} as Any

if (isFoobar(value)) {
    let a = value.kind === 'foo'
    ? value.kind
    : value.value.kind
}

πŸ™ Actual behavior & πŸ™‚ Expected behavior

isFoobar narrows the type to only Foo but should narrow it to the specified Foo | Bar<Foo>. It seems to be related to the combination of the generic parameter and the circularity because I can not find a way to simplify it further.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions