TS: Use contextual typing for literals

This commit is contained in:
Asger F
2019-04-05 18:40:07 +01:00
parent d7bfeeefd0
commit 50c2921625
2 changed files with 35 additions and 8 deletions

View File

@@ -176,7 +176,10 @@ export function augmentAst(ast: AugmentedSourceFile, code: string, project: Proj
if (typeChecker != null) {
if (isTypedNode(node)) {
let type = typeChecker.getTypeAtLocation(node);
let contextualType = isContextuallyTypedNode(node)
? typeChecker.getContextualType(node)
: null;
let type = contextualType || typeChecker.getTypeAtLocation(node);
if (type != null) {
let id = typeTable.buildType(type);
if (id != null) {
@@ -326,3 +329,10 @@ function isTypedNode(node: ts.Node): boolean {
return ts.isTypeNode(node);
}
}
type ContextuallyTypedNode = (ts.ArrayLiteralExpression | ts.ObjectLiteralExpression) & AugmentedNode;
function isContextuallyTypedNode(node: ts.Node): node is ContextuallyTypedNode {
let kind = node.kind;
return kind === ts.SyntaxKind.ArrayLiteralExpression || kind === ts.SyntaxKind.ObjectLiteralExpression;
}