Property assignment uses parent type annotation (#32553)

* Property assignment uses parent type annotation

First draft, will write full explanation later.

Also makes sure that jsdoc is ignored in TS. It was not before.

* Update baselines
This commit is contained in:
Nathan Shively-Sanders
2019-07-25 10:23:03 -07:00
committed by GitHub
parent aa12ec440c
commit 772bee5e84
11 changed files with 250 additions and 11 deletions
+16 -9
View File
@@ -5396,7 +5396,7 @@ namespace ts {
return undefined;
}
function getWidenedTypeFromAssignmentDeclaration(symbol: Symbol, resolvedSymbol?: Symbol) {
function getWidenedTypeForAssignmentDeclaration(symbol: Symbol, resolvedSymbol?: Symbol) {
// function/class/{} initializers are themselves containers, so they won't merge in the same way as other initializers
const container = getAssignedExpandoInitializer(symbol.valueDeclaration);
if (container) {
@@ -5429,7 +5429,7 @@ namespace ts {
}
}
if (!isCallExpression(expression)) {
jsdocType = getJSDocTypeFromAssignmentDeclaration(jsdocType, expression, symbol, declaration);
jsdocType = getAnnotatedTypeForAssignmentDeclaration(jsdocType, expression, symbol, declaration);
}
if (!jsdocType) {
(types || (types = [])).push((isBinaryExpression(expression) || isCallExpression(expression)) ? getInitializerTypeFromAssignmentDeclaration(symbol, resolvedSymbol, expression, kind) : neverType);
@@ -5478,8 +5478,8 @@ namespace ts {
return type;
}
function getJSDocTypeFromAssignmentDeclaration(declaredType: Type | undefined, expression: Expression, _symbol: Symbol, declaration: Declaration) {
const typeNode = getJSDocType(expression.parent);
function getAnnotatedTypeForAssignmentDeclaration(declaredType: Type | undefined, expression: Expression, symbol: Symbol, declaration: Declaration) {
const typeNode = getEffectiveTypeAnnotationNode(expression.parent);
if (typeNode) {
const type = getWidenedType(getTypeFromTypeNode(typeNode));
if (!declaredType) {
@@ -5489,6 +5489,13 @@ namespace ts {
errorNextVariableOrPropertyDeclarationMustHaveSameType(/*firstDeclaration*/ undefined, declaredType, declaration, type);
}
}
if (symbol.parent) {
const typeNode = getEffectiveTypeAnnotationNode(symbol.parent.valueDeclaration);
if (typeNode) {
return getTypeOfPropertyOfType(getTypeFromTypeNode(typeNode), symbol.escapedName);
}
}
return declaredType;
}
@@ -5783,7 +5790,7 @@ namespace ts {
}
else if (isInJSFile(declaration) &&
(isCallExpression(declaration) || isBinaryExpression(declaration) || isPropertyAccessExpression(declaration) && isBinaryExpression(declaration.parent))) {
type = getWidenedTypeFromAssignmentDeclaration(symbol);
type = getWidenedTypeForAssignmentDeclaration(symbol);
}
else if (isJSDocPropertyLikeTag(declaration)
|| isPropertyAccessExpression(declaration)
@@ -5798,7 +5805,7 @@ namespace ts {
return getTypeOfFuncClassEnumModule(symbol);
}
type = isBinaryExpression(declaration.parent) ?
getWidenedTypeFromAssignmentDeclaration(symbol) :
getWidenedTypeForAssignmentDeclaration(symbol) :
tryGetTypeFromEffectiveTypeNode(declaration) || anyType;
}
else if (isPropertyAssignment(declaration)) {
@@ -5969,7 +5976,7 @@ namespace ts {
}
else if (declaration.kind === SyntaxKind.BinaryExpression ||
declaration.kind === SyntaxKind.PropertyAccessExpression && declaration.parent.kind === SyntaxKind.BinaryExpression) {
return getWidenedTypeFromAssignmentDeclaration(symbol);
return getWidenedTypeForAssignmentDeclaration(symbol);
}
else if (symbol.flags & SymbolFlags.ValueModule && declaration && isSourceFile(declaration) && declaration.commonJsModuleIndicator) {
const resolvedModule = resolveExternalModuleSymbol(symbol);
@@ -5978,7 +5985,7 @@ namespace ts {
return errorType;
}
const exportEquals = getMergedSymbol(symbol.exports!.get(InternalSymbolName.ExportEquals)!);
const type = getWidenedTypeFromAssignmentDeclaration(exportEquals, exportEquals === resolvedModule ? undefined : resolvedModule);
const type = getWidenedTypeForAssignmentDeclaration(exportEquals, exportEquals === resolvedModule ? undefined : resolvedModule);
if (!popTypeResolution()) {
return reportCircularityError(symbol);
}
@@ -19157,7 +19164,7 @@ namespace ts {
}
/**
* Woah! Do you really want to use this function?
* Whoa! Do you really want to use this function?
*
* Unless you're trying to get the *non-apparent* type for a
* value-literal type or you're authoring relevant portions of this algorithm,