jasnell / proposal-istypes

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

What about Error family? #15

Closed micnic closed 7 years ago

micnic commented 7 years ago

Should EvalError.isEvalError(), RangeError.isRangeError(), ReferenceError.isReferenceError(), SyntaxError.isSyntaxError(), TypeError.isTypeError() and URIError.isURIError() be also part of this proposal?

zloirock commented 7 years ago

I'm not sure about it. It's just subclasses of Error with the same [[ErrorData]] internal slot.

jasnell commented 7 years ago

These, I think would be quite a bit harder, and a similar check can be done simply by calling (for instance) Error.isError(err) && err.name === 'RangeError'

ljharb commented 7 years ago

I think the proposal will be much simpler if it serves to expose boolean-returning methods that do brand checks - ie, check for the presence of an internal slot. That means it could detect Typed Arrays but not tell you which one is which, and the same for Errors.

doodadjs commented 7 years ago

Maybe a single "is" function with the constructor passed as argument ?

Object.is("", String)   // true
Object.is(1, Number)   // true
Object.is(new TypeError(), Error) // true
Object.is(new TypeError(), TypeError) // true
// ...
ljharb commented 7 years ago

@doodadjs nope, that's a non-starter, since constructor comparisons won't work cross-realm (like iframes etc). That's the primary reason instanceof isn't viable.

ljharb commented 7 years ago

(Separately, Object.is already exists to expose the spec operation SameValue)

doodadjs commented 7 years ago

@ljharb Yeah, I know. I mean by using internal slots (if that's possible).

EDIT: That could be "Object.check" or another name.

ljharb commented 7 years ago

Error is a function and does not have an [[ErrorData]] internal slot - and there's no way to know if a given function would or would not imbue any particular internal slots.

jasnell commented 7 years ago

I believe the current proposal handles this adequately