jasnell / proposal-istypes

TC-39 Proposal for additional is{Type} APIs
201 stars 7 forks source link

Regarding `typeOf` examples #38

Open brettz9 opened 6 years ago

brettz9 commented 6 years ago

A few different observations or suggestions regarding the typeOf examples:

  1. new WebAssembly.Module() would I guess give the same as the Module exotic object:
    import * as util from './util.js';`
    Builtin.typeOf(util);

    ... so maybe the above should be indicated as such (if not differentiated)?

  2. I think NaN and arguments are interesting enough to describe their expected values
  3. I'd be curious to know whether it would be expected that host environments like browsers would end up making DOM objects return their name (e.g., HTMLSelectElement), and if so, maybe raising an example possibility would even better demonstrate the potential.
ljharb commented 6 years ago
  1. NaN is a primitive; i think Number.isNaN is sufficient to detect that. arguments already has a ToStringTag, so there's no reason it wouldn't be able to report "Arguments" here.

  2. yes, just like ToStringTag (which was primarily added for HTML elements, as I understand it), this would allow engines to do the same.

brettz9 commented 6 years ago

Ah yes, re: arguments, thank you.

While NaN can be detected by Number.isNaN, it seems unnecessarily cumbersome (and making for less immediately readable code) to need to add a special type-checking case for it; moreover, I'd expect most uses of type-based switch statements would not be seeking to treat NaN the same as number (and if they were, they could easily add another fall-through case to handle both together).

ljharb commented 6 years ago

the type of NaN is number; it wouldn't be appropriate for something called "typeOf" to return anything other than "number" for it.

brettz9 commented 6 years ago

The typeof of null is, and with this proposal, remains, "object" as well. There can be different types of types. :-)

brettz9 commented 6 years ago

And looking at it semantically, "not a number" is not a number. :-)

ljharb commented 6 years ago

NaN represents “no number”, just like null represents “no object” ¯\_(ツ)_/¯

brettz9 commented 6 years ago

Despite the spec naming it "Not-a-Number", I think that is a fair characterization. However, again, the proposal currently (and thankfully) distinguishes null from "object".

ljharb commented 6 years ago

Indeed; this is more akin to Object.prototype.toString.call(null).slice(8, -1) :-)