diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 21707940a5a..bb866c6b8cb 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4730,13 +4730,16 @@ module ts { if (!inferredType) { let inferences = getInferenceCandidates(context, index); if (inferences.length) { - // Infer widened union or supertype, or the undefined type for no common supertype + // Infer widened union or supertype, or the unknown type for no common supertype let unionOrSuperType = context.inferUnionTypes ? getUnionType(inferences) : getCommonSupertype(inferences); inferredType = unionOrSuperType ? getWidenedType(unionOrSuperType) : unknownType; inferenceSucceeded = !!unionOrSuperType; } else { - // Infer the empty object type when no inferences were made + // Infer the empty object type when no inferences were made. It is important to remember that + // in this case, inference still succeeds, meaning there is no error for not having inference + // candidates. An inference error only occurs when there are *conflicting* candidates, i.e. + // candidates with no common supertype. inferredType = emptyObjectType; inferenceSucceeded = true; } @@ -4746,10 +4749,10 @@ module ts { let constraint = getConstraintOfTypeParameter(context.typeParameters[index]); inferredType = constraint && !isTypeAssignableTo(inferredType, constraint) ? constraint : inferredType; } - // If inference failed, it is necessary to record the index of the failed type parameter (the one we are on). - // It might be that inference has already failed on a later type parameter on a previous call to inferTypeArguments. - // So if this failure is on preceding type parameter, this type parameter is the new failure index. else if (context.failedTypeParameterIndex === undefined || context.failedTypeParameterIndex > index) { + // If inference failed, it is necessary to record the index of the failed type parameter (the one we are on). + // It might be that inference has already failed on a later type parameter on a previous call to inferTypeArguments. + // So if this failure is on preceding type parameter, this type parameter is the new failure index. context.failedTypeParameterIndex = index; } context.inferredTypes[index] = inferredType; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 5880dfdce92..403c96b239b 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1482,6 +1482,7 @@ module ts { (t: Type): Type; } + // @internal export interface TypeInferences { primary: Type[]; // Inferences made directly to a type parameter secondary: Type[]; // Inferences made to a type parameter in a union type @@ -1489,6 +1490,7 @@ module ts { // If a type parameter is fixed, no more inferences can be made for the type parameter } + // @internal export interface InferenceContext { typeParameters: TypeParameter[]; // Type parameters for which inferences are made inferUnionTypes: boolean; // Infer union types for disjoint candidates (otherwise undefinedType) diff --git a/tests/baselines/reference/APISample_compile.js b/tests/baselines/reference/APISample_compile.js index d08d0b3fcc2..e2c0341d5dd 100644 --- a/tests/baselines/reference/APISample_compile.js +++ b/tests/baselines/reference/APISample_compile.js @@ -1172,18 +1172,6 @@ declare module "typescript" { interface TypeMapper { (t: Type): Type; } - interface TypeInferences { - primary: Type[]; - secondary: Type[]; - isFixed: boolean; - } - interface InferenceContext { - typeParameters: TypeParameter[]; - inferUnionTypes: boolean; - inferences: TypeInferences[]; - inferredTypes: Type[]; - failedTypeParameterIndex?: number; - } interface DiagnosticMessage { key: string; category: DiagnosticCategory; diff --git a/tests/baselines/reference/APISample_compile.types b/tests/baselines/reference/APISample_compile.types index df0c57444b0..12803044f6e 100644 --- a/tests/baselines/reference/APISample_compile.types +++ b/tests/baselines/reference/APISample_compile.types @@ -3756,41 +3756,6 @@ declare module "typescript" { >t : Type >Type : Type >Type : Type - } - interface TypeInferences { ->TypeInferences : TypeInferences - - primary: Type[]; ->primary : Type[] ->Type : Type - - secondary: Type[]; ->secondary : Type[] ->Type : Type - - isFixed: boolean; ->isFixed : boolean - } - interface InferenceContext { ->InferenceContext : InferenceContext - - typeParameters: TypeParameter[]; ->typeParameters : TypeParameter[] ->TypeParameter : TypeParameter - - inferUnionTypes: boolean; ->inferUnionTypes : boolean - - inferences: TypeInferences[]; ->inferences : TypeInferences[] ->TypeInferences : TypeInferences - - inferredTypes: Type[]; ->inferredTypes : Type[] ->Type : Type - - failedTypeParameterIndex?: number; ->failedTypeParameterIndex : number } interface DiagnosticMessage { >DiagnosticMessage : DiagnosticMessage diff --git a/tests/baselines/reference/APISample_linter.js b/tests/baselines/reference/APISample_linter.js index fb390d78f8f..265d84ac5b3 100644 --- a/tests/baselines/reference/APISample_linter.js +++ b/tests/baselines/reference/APISample_linter.js @@ -1203,18 +1203,6 @@ declare module "typescript" { interface TypeMapper { (t: Type): Type; } - interface TypeInferences { - primary: Type[]; - secondary: Type[]; - isFixed: boolean; - } - interface InferenceContext { - typeParameters: TypeParameter[]; - inferUnionTypes: boolean; - inferences: TypeInferences[]; - inferredTypes: Type[]; - failedTypeParameterIndex?: number; - } interface DiagnosticMessage { key: string; category: DiagnosticCategory; diff --git a/tests/baselines/reference/APISample_linter.types b/tests/baselines/reference/APISample_linter.types index 90360b5fe61..f818edbaf39 100644 --- a/tests/baselines/reference/APISample_linter.types +++ b/tests/baselines/reference/APISample_linter.types @@ -3902,41 +3902,6 @@ declare module "typescript" { >t : Type >Type : Type >Type : Type - } - interface TypeInferences { ->TypeInferences : TypeInferences - - primary: Type[]; ->primary : Type[] ->Type : Type - - secondary: Type[]; ->secondary : Type[] ->Type : Type - - isFixed: boolean; ->isFixed : boolean - } - interface InferenceContext { ->InferenceContext : InferenceContext - - typeParameters: TypeParameter[]; ->typeParameters : TypeParameter[] ->TypeParameter : TypeParameter - - inferUnionTypes: boolean; ->inferUnionTypes : boolean - - inferences: TypeInferences[]; ->inferences : TypeInferences[] ->TypeInferences : TypeInferences - - inferredTypes: Type[]; ->inferredTypes : Type[] ->Type : Type - - failedTypeParameterIndex?: number; ->failedTypeParameterIndex : number } interface DiagnosticMessage { >DiagnosticMessage : DiagnosticMessage diff --git a/tests/baselines/reference/APISample_transform.js b/tests/baselines/reference/APISample_transform.js index 96b54ccd917..f1da0e2533a 100644 --- a/tests/baselines/reference/APISample_transform.js +++ b/tests/baselines/reference/APISample_transform.js @@ -1204,18 +1204,6 @@ declare module "typescript" { interface TypeMapper { (t: Type): Type; } - interface TypeInferences { - primary: Type[]; - secondary: Type[]; - isFixed: boolean; - } - interface InferenceContext { - typeParameters: TypeParameter[]; - inferUnionTypes: boolean; - inferences: TypeInferences[]; - inferredTypes: Type[]; - failedTypeParameterIndex?: number; - } interface DiagnosticMessage { key: string; category: DiagnosticCategory; diff --git a/tests/baselines/reference/APISample_transform.types b/tests/baselines/reference/APISample_transform.types index 999c81af401..6f22fb1ec6a 100644 --- a/tests/baselines/reference/APISample_transform.types +++ b/tests/baselines/reference/APISample_transform.types @@ -3852,41 +3852,6 @@ declare module "typescript" { >t : Type >Type : Type >Type : Type - } - interface TypeInferences { ->TypeInferences : TypeInferences - - primary: Type[]; ->primary : Type[] ->Type : Type - - secondary: Type[]; ->secondary : Type[] ->Type : Type - - isFixed: boolean; ->isFixed : boolean - } - interface InferenceContext { ->InferenceContext : InferenceContext - - typeParameters: TypeParameter[]; ->typeParameters : TypeParameter[] ->TypeParameter : TypeParameter - - inferUnionTypes: boolean; ->inferUnionTypes : boolean - - inferences: TypeInferences[]; ->inferences : TypeInferences[] ->TypeInferences : TypeInferences - - inferredTypes: Type[]; ->inferredTypes : Type[] ->Type : Type - - failedTypeParameterIndex?: number; ->failedTypeParameterIndex : number } interface DiagnosticMessage { >DiagnosticMessage : DiagnosticMessage diff --git a/tests/baselines/reference/APISample_watcher.js b/tests/baselines/reference/APISample_watcher.js index 8a3d2065a23..734467a1c59 100644 --- a/tests/baselines/reference/APISample_watcher.js +++ b/tests/baselines/reference/APISample_watcher.js @@ -1241,18 +1241,6 @@ declare module "typescript" { interface TypeMapper { (t: Type): Type; } - interface TypeInferences { - primary: Type[]; - secondary: Type[]; - isFixed: boolean; - } - interface InferenceContext { - typeParameters: TypeParameter[]; - inferUnionTypes: boolean; - inferences: TypeInferences[]; - inferredTypes: Type[]; - failedTypeParameterIndex?: number; - } interface DiagnosticMessage { key: string; category: DiagnosticCategory; diff --git a/tests/baselines/reference/APISample_watcher.types b/tests/baselines/reference/APISample_watcher.types index f66c818c53d..37940e9a1f8 100644 --- a/tests/baselines/reference/APISample_watcher.types +++ b/tests/baselines/reference/APISample_watcher.types @@ -4025,41 +4025,6 @@ declare module "typescript" { >t : Type >Type : Type >Type : Type - } - interface TypeInferences { ->TypeInferences : TypeInferences - - primary: Type[]; ->primary : Type[] ->Type : Type - - secondary: Type[]; ->secondary : Type[] ->Type : Type - - isFixed: boolean; ->isFixed : boolean - } - interface InferenceContext { ->InferenceContext : InferenceContext - - typeParameters: TypeParameter[]; ->typeParameters : TypeParameter[] ->TypeParameter : TypeParameter - - inferUnionTypes: boolean; ->inferUnionTypes : boolean - - inferences: TypeInferences[]; ->inferences : TypeInferences[] ->TypeInferences : TypeInferences - - inferredTypes: Type[]; ->inferredTypes : Type[] ->Type : Type - - failedTypeParameterIndex?: number; ->failedTypeParameterIndex : number } interface DiagnosticMessage { >DiagnosticMessage : DiagnosticMessage