Answer the question
In order to leave comments, you need to log in
Typescript and types by conditions. How to do it right?
There are several ITreeNode interfaces and IIdentifierNode inherited from it and a few others, etc.
There is a code:
estraverse.replace(catchClauseNode.param, {
leave: (node: ITreeNode, parentNode: ITreeNode) => {
switch (node.type) {
case 'Identifier':
this.catchClauseParam.set(node.name, Utils.getRandomVariableName());
node.name = this.catchClauseParam.get(node.name);
break;
default:
return estraverse.VisitorOption.Skip;
}
}
});
Answer the question
In order to leave comments, you need to log in
Made so far through user defined guard functions:
https://github.com/Microsoft/TypeScript/issues/1007
public static isIdentifierNode (node: ITreeNode): node is IIdentifierNode {
return node.type === 'Identifier';
}
estraverse.replace(catchClauseNode.param, {
leave: (node: ITreeNode, parentNode: ITreeNode) => {
if (NodeUtils.isIdentifierNode(node)) {
this.catchClauseParam.set(node.name, Utils.getRandomVariableName());
node.name = this.catchClauseParam.get(node.name);
return;
}
return estraverse.VisitorOption.Skip;
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question