#2574·rollup

Don’t consider “instanceof Foo” to be a use of Foo for tree-shaking purposes

Author: chris-morganCreated Dec 1, 2018Updated Jul 18, 2026
Labelst³ ✨ enhancements² 🔥🔥 important

Feature Use Case

In the Overture library we make/use at FastMail, we have certain features that might not actually be used, but because certain other parts of the library need to handle them specially. For example, O.RichTextView, which is one of the larger single components: I have a project that’s not using it, but it’s necessarily included because O.GlobalKeyboardShortcuts needs to check if it’s working with a RichTextView, which is does with instanceof.

The current workaround to avoid this loading is to define an extra property that can cheaply be checked, e.g. isRichTextView, and check that; but that’s more verbose and less obvious for code tracing purposes, and it’s nice to be able to just use instanceof like normal.

Feature Proposal

When tree-shaking, don’t consider object instanceof constructor to be a use of constructor; and if constructor is then removed as dead code, replace the entire instanceof expression with (object, false).

This is reasonable because if you never use/instantiate a type, instanceof will always evaluate to false. All that remains is that you must evaluate its left hand side, which may, after all, be fallible or have side-effects. In most cases, instanceof is immediately used for branching, and so the false resolution allows that code to then be considered dead. A trivial left hand side will also be removed.

Demonstration:

Code with instanceof: what is written.

Code with the instanceof check replaced: what it would become equivalent to, under this proposal.