TypeScript · Senior · Question 62 of 75
What does a this is predicate do in a class method?
class Node {
isLeaf(): this is Leaf { return this.children.length === 0; }
}- ✓Correct answer: It narrows the object the method was called on when the method returns
true - 2It changes
thisat runtime - 3It makes the method static
Explanation
Calling if (node.isLeaf()) narrows node inside the branch, like a regular type guard.