Delay pulling on signature contextual type until absolutely needed (#37851)

This commit is contained in:
Wesley Wigham
2020-04-21 13:29:45 -07:00
committed by GitHub
parent 136f728bb0
commit a72e49e875
6 changed files with 131 additions and 8 deletions

View File

@@ -55,7 +55,9 @@ namespace ts {
const enum WideningKind {
Normal,
GeneratorYield
FunctionReturn,
GeneratorNext,
GeneratorYield,
}
const enum TypeFacts {
@@ -18092,7 +18094,7 @@ namespace ts {
}
function reportErrorsFromWidening(declaration: Declaration, type: Type, wideningKind?: WideningKind) {
if (produceDiagnostics && noImplicitAny && getObjectFlags(type) & ObjectFlags.ContainsWideningType) {
if (produceDiagnostics && noImplicitAny && getObjectFlags(type) & ObjectFlags.ContainsWideningType && (!wideningKind || !getContextualSignatureForFunctionLikeDeclaration(declaration as FunctionLikeDeclaration))) {
// Report implicit any error within type if possible, otherwise report error on declaration
if (!reportWideningErrorsInType(type)) {
reportImplicitAny(declaration, type, wideningKind);
@@ -27047,15 +27049,13 @@ namespace ts {
}
if (returnType || yieldType || nextType) {
const contextualSignature = getContextualSignatureForFunctionLikeDeclaration(func);
if (!contextualSignature) {
if (yieldType) reportErrorsFromWidening(func, yieldType, WideningKind.GeneratorYield);
if (returnType) reportErrorsFromWidening(func, returnType);
if (nextType) reportErrorsFromWidening(func, nextType);
}
if (yieldType) reportErrorsFromWidening(func, yieldType, WideningKind.GeneratorYield);
if (returnType) reportErrorsFromWidening(func, returnType, WideningKind.FunctionReturn);
if (nextType) reportErrorsFromWidening(func, nextType, WideningKind.GeneratorNext);
if (returnType && isUnitType(returnType) ||
yieldType && isUnitType(yieldType) ||
nextType && isUnitType(nextType)) {
const contextualSignature = getContextualSignatureForFunctionLikeDeclaration(func);
const contextualType = !contextualSignature ? undefined :
contextualSignature === getSignatureFromDeclaration(func) ? isGenerator ? undefined : returnType :
instantiateContextualType(getReturnTypeOfSignature(contextualSignature), func);