Skip to content

No implicit returns error in exhaustive if statement #17358

Closed
@OliverJAsh

Description

@OliverJAsh

TS 2.4.1 with noImplicitReturns enabled

    class A {}
    class B {}

    // error: Not all code paths return a value.
    const fn = (x: A | B) => {
        if (x instanceof A) {
            x // A
            return 1
        } else if (x instanceof B) {
            x // B
            return 2
        } else {
            x // never
        }
    }

I would not expect this to error because the if statement is exhaustive, therefore all code paths do return a value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions