The TypeScript playground encounters an issue with the Symbol.hasInstance
built-in symbol, while it functions properly for other symbols.
Testing other symbol methods such as Symbol.match
and Symbol.replace
show no problems, but Symbol.hasInstance
is not returning the expected result despite being identified correctly in console.log messages.
I have tried running the code on both the TypeScript playground and MDN, but they both return false with the generated code.
If you wish to try this code yourself, simply paste it into the TypeScript playground at https://www.typescriptlang.org/play/index.html
class Array1 {
static [Symbol.hasInstance](instance) {
console.log(instance);
return Array.isArray(instance);
}
}
console.log(Symbol.hasInstance.toString());
let arr: string[] = ['a', 'b'];
console.log(arr instanceof Array1);
console.log([] instanceof Array1);
// expected output: true
// current output is false
Upon clicking "run," the generated code fails to return true on the left-hand side.
var Array1 = /** @class */ (function () {
function Array1() {
}
Array1[Symbol.hasInstance] = function (instance) {
console.log(instance);
return Array.isArray(instance);
};
return Array1;
}());
console.log(Symbol.hasInstance.toString());
var arr = ['a', 'b'];
console.log(arr instanceof Array1);
console.log([] instanceof Array1);
It should be returning true instead of false.