refactor(compiler-cli): remove undesirable cast in the type translator (#38775)

The cast to `ts.Identifier` was a hack that "just happened to work".
The new approach is more robust and doesn't have to undermine
the type checker.

PR Close #38775
This commit is contained in:
Pete Bacon Darwin 2020-09-21 12:34:07 +01:00 committed by Misko Hevery
parent 6ae3b68acf
commit 856e74ac98
1 changed files with 6 additions and 3 deletions

View File

@ -742,9 +742,12 @@ export class TypeTranslatorVisitor implements ExpressionVisitor, TypeVisitor {
}
visitTypeofExpr(ast: TypeofExpr, context: Context): ts.TypeQueryNode {
let expr = translateExpression(
ast.expr, this.imports, NOOP_DEFAULT_IMPORT_RECORDER, ts.ScriptTarget.ES2015);
return ts.createTypeQueryNode(expr as ts.Identifier);
const typeNode = this.translateExpression(ast.expr, context);
if (!ts.isTypeReferenceNode(typeNode)) {
throw new Error(`The target of a typeof expression must be a type reference, but it was
${ts.SyntaxKind[typeNode.kind]}`);
}
return ts.createTypeQueryNode(typeNode.typeName);
}
private translateType(type: Type, context: Context): ts.TypeNode {