I'm having issues using the instanceof operator and it doesn't seem to work. Here is a part of my code:
const results = _.map(items, function(item: Goal|Note|Task, index: number) {
let result = {};
if (item instanceof Goal) {
result = { id: index, title: item.name };
} else if (item instanceof Note) {
result = { id: index, title: item.content.text };
} else if (item instanceof Task) {
result = { id: index, title: item.name };
}
console.log(item);
console.log(item instanceof Goal);
console.log(item instanceof Note);
console.log(item instanceof Task);
return result;
});
All of my logs say false, here is what the console looks like:
None of them match, despite being explicit that only the 3 types would be possible. You could also see the object itself with a typename of Goal, so I don't get why it doesn't match with instanceof Goal.
Any ideas?
items
? Are they created through constructors? If not, they won't be instances of a given class. – Proceedinginstanceof
to work, you need to actual make them from constructors. Otherwise they're just objects that happen to have the same shape as your desired objects. – Proceeding