镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

TypeScript should respect "instanceof" in logical expressions #31291

Description

@slavafomin

Hello!

Thank you for your hard work.

Please, consider this code:

class Foo { 
    foo: string;
}
class Bar {
    bar: string;
}

function foo(value: Foo | Bar) {
    const isFoo = value instanceof Foo;
    const isBar = !isFoo && value.bar; // <-- causes error
}

The line above throws an error:

Property 'bar' does not exist on type 'Foo | Bar'.
Property 'bar' does not exist on type 'Foo'.

However, if I replace the logical variable with the expression directly, it works:

const isBar = !(value instanceof Foo) && value.bar;

The isFoo variable in my example is a boolean constant, it's value can't change, so I believe it should be statically analyzable.

Activity

  1. j-oliveras commented on May 7, 2019

    @j-oliveras
    Contributor

    Typescript don't track related variables.

  2. JakeTunaley commented on May 8, 2019

    @JakeTunaley

    Duplicate of #12184

  3. typescript-bot commented on May 11, 2019

    @typescript-bot
    Contributor

    This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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