Strawman proposal: new `is` operator

Isiah Meadows impinball at
Sun Aug 24 13:04:51 PDT 2014

There is no way currently to reliably verify types in JS. Type annotations
cannot solve this completely, especially when you have an overloaded
function. A way to reliably verify types would make this far easier. So
far, here are the current options with their respective caveats:

1. `instanceof` - This returns true for the class, but also for subclasses.
This isn't always desired.

2. `this.contructor === Foo` - The constructor property could easily be
overwritten, rendering this relatively useless.

3. `typeof` - This only is useful for literals, with the exception of

4. `` - This returns the correct native
object type for native objects, but returns `[object Object]` for all
non-native objects, rendering it effectively useless. This includes object
instances created from `class Foo {}`, etc.

This proposed operator, `is`, would basically use the same algorithm for
the `instanceof` operator, but instead of walking up the prototype chain
iteratively, it would only check the first level of the right side. The
static and runtime semantics would be equal to that of the `instanceof`

It would allow for simpler overloading, simpler assertions, simpler type
checks, and simpler testing.

Example usage:

    class Foo {}
    class Bar extends Foo {}

    const assert = (message, test) => {
      if (!test) {
        throw new TypeError(message);

    let obj = new Bar();

    // none of these should throw
    assert('obj is of type Bar', obj is Bar);
    assert('obj is not of type Foo', !(obj is Foo));

One big question I have is whether a different keyword should be used
(`isa`, etc), given its use as a keyword synonym to `===` in many
compile-to-JS languages, most notably CoffeeScript.

Isiah Meadows
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list