microsoft / microsoft/TypeScript

Flag 'instanceof' expressions that are provably always true or false

Open
#32,801 6 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Suggestion

When refactoring code, TypeScript is generally very good at finding and reporting situations where existing code will break due to type changes. This allows "refactoring with confidence". This isn't surprising given that one of the primary goals of TypeScript is to "statically identify constructs that are likely to be errors".

There's one common case where TypeScript is "blind" and doesn't report errors when types are refactored. This case involves the use of the 'instanceof' operator. This operator can be used in situations where it makes no sense -- e.g. where the type specified on the LHS has no possible relation to the type specified on the RHS.

To help developers avoid this common programming error, I propose that TypeScript report an error for any 'instanceof' expression that is provably always true or false at compilation time. Any such expression will likely be unintended by the programmer and should be flagged as errors. At best, such operations represent unnecessary code that imposes runtime overhead.

Example

// ClassB is a subclass of ClassA.
class ClassA {}
class ClassB extends ClassA {}

// ClassC has nothing to do with either
// ClassA or ClassB.
class ClassC {
    method1() {}
}

function function1(param1: ClassB) {
    // Static analysis can prove this expression
    // will always evaluate to false. It's a common
    // source of programming errors, especially during
    // refactoring.
    if (param1 instanceof ClassC) {
        // This call isn't even valid given that param1
        // was declared as type ClassB.
        param1.method1();
    }

    // Static analysis can prove this expression will
    // always evaluate to true, so at best it's unnecessary
    // runtime overhead and more commonly is a behavior
    // that is unintended by the programmer.
    if (param1 instanceof ClassA) {
        // ...
    }
}

Are there any legitimate uses of 'instanceof' where the result is statically provable to be true or false in all cases? I can't think of any, but it's possible I'm overlooking some specialized cases. If there are, this check could be added as an optional compiler switch to preserve the current behavior.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the issue's ClassA, ClassB, and ClassC examples and the existing instanceof type-checking behavior in the TypeScript compiler. Determine how always-true and always-false cases should be diagnosed, including any legitimate exceptions. Done means the compiler reports the intended cases with coverage for the examples and preserves valid instanceof usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.