Typescript Instanceof Not Working
Solution 1:
instanceof
will return true only if it matches the function or class from which it was constructed. The item
here is a plain Object
.
const a = { a: 1 } // plain objectconsole.log(a);
// {a:1} <-- the constructor type is empty// a: 1// __proto__: Object <-- inherited from
a instanceof A // false because it is a plain object
a instanceofObject// true because all object are inherited from Object
If it is constructed using a constructor function or a class, then instanceof will work as expected:
functionA(a) {
this.a = a;
}
const a = newA(1); // create new "instance of" Aconsole.log(a);
// A {a:1} <-- the constructor type is `A`
a instanceof A // true because it is constructed from A
a instanceofObject// true
If Goal
is an Interface
it will only check the structure of the object not its type. If Goal
is a constructor then it should return true for instanceof
checks.
Try something like:
// interface Goal {...}classGoal {...} // you will have to change the way it works.
items = [
newGoal()
];
Update 2021:
Been playing with Typescript recently and came up with a better solution that works both in Typescript and JavaScript:
Try something like:
interfaceGoal {
getCount(): number;
}
classGoalimplementsGoal {
getCount() {
return3;
}
}
functiongetItemCount(item: Goal | Note | Task) {
return item instanceofGoal ? item.getCount() : 'not a goal';
}
console.log(getItemCount(newGoal())); // 3console.log(getItemCount('goal')); // 'not a goal';
Here the interface and the class has the same name, so they can be used as both and type and an instance checker.
Changing the interface Goal
signature or class Goal
signature would throw something like:
TS2394: This overload signature isnot compatible with its implementation signature.
Solution 2:
You can also use type guards to your advantage:
https://basarat.gitbooks.io/typescript/docs/types/typeGuard.html
https://www.typescriptlang.org/docs/handbook/advanced-types.html
For instance, if you use a literal type guard to your class:
classGoal {
type: 'goal'
...
}
then the check is as simple as :
if (item.type === 'goal') {
}
Or you could write your own type guards:
function isNote(arg: any): arg is Note {// because only your Note class has "content" property?return arg.content !== undefined;
}
if (isNote(item)) {
result = { id: index, title: item.content.text };
}
Solution 3:
Try to instantiate the object with a constructor. It happened to me the same because I was manually mocking the object for testing purposes. If you create the item like following example, it should work:
item: Goal =new Goal(*item values*)
Solution 4:
as @Gopikrishna pointed out, object parsed from JSON.parse
or received from API will not match your custom Class
because it is not created via new
operator of desired class.
One workaround is to first cast object to the desired class and then check for a property against undefined
.
classUser{
displayName:string;
id:string;
}
const user = obj asUser;
if (user.displayName!==undefined) {
//do your thing here
}
Post a Comment for "Typescript Instanceof Not Working"