Array.isTypedArray()

Someone added a comment to my old answer on StackOverflow question on how to check if an object is a TypedArray.

https://stackoverflow.com/questions/15251879/how-to-check-if-a-variable-is-a-typed-array-in-javascript/65427116?noredirect=1#comment141209386_65427116

My suggestion was:

const isTypedArray = (function() {
  const TypedArray = Object.getPrototypeOf(Uint8Array);
  return (obj) => obj instanceof TypedArray;
})();

The problem with this, as pointed out in the comment, is that it will not work with a different realm.

Similar to object instanceof Array.

There should be a way to check if the object is a TypedArray and nothing else; that will work cross realm, similar to Array.isArray.

You can use TypedArray.prototype[Symbol.toStringTag].

const getTypedArrayName = Object.getOwnPropertyDescriptor(
  Object.getPrototypeOf(Int8Array.prototype),
  Symbol.toStringTag,
).get;

console.log(getTypedArrayName.call(new Int32Array())); // "Int32Array"
console.log(getTypedArrayName.call(new Float64Array())); // "Float64Array"
console.log(getTypedArrayName.call([])); // undefined

That's essentially the implementation of https://npmjs.com/which-typed-array and https://npmjs.com/is-typed-array .

If sufficient use cases were available, I'd be happy to make a proposal for the predicate.