Skip to content

Commit e5b568f

Browse files
committed
Consistently propagate 'any' and 'never' types in type inference
1 parent c0c3f4a commit e5b568f

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11490,20 +11490,21 @@ namespace ts {
1149011490
let symbolStack: Symbol[];
1149111491
let visited: Map<boolean>;
1149211492
let contravariant = false;
11493+
let propagationType: Type;
1149311494
inferFromTypes(originalSource, originalTarget);
1149411495

1149511496
function inferFromTypes(source: Type, target: Type) {
1149611497
if (!couldContainTypeVariables(target)) {
1149711498
return;
1149811499
}
11499-
if (source === neverType || source === wildcardType) {
11500-
// We are inferring from 'never' or the wildcard type. We want to infer this
11501-
// type for every type parameter referenced in the target type, so we infer from
11502-
// target to itself with a flag we check when recording candidates.
11503-
const savePriority = priority;
11504-
priority |= source === neverType ? InferencePriority.Never : InferencePriority.Wildcard;
11500+
if (source.flags & (TypeFlags.Any | TypeFlags.Never) && source !== silentNeverType) {
11501+
// We are inferring from 'any' or 'never'. We want to infer this type for every type parameter
11502+
// referenced in the target type, so we record the propagation type and infer from the target
11503+
// to itself. Then, as we find candidates we substitute the propagation type.
11504+
const savePropagationType = propagationType;
11505+
propagationType = source;
1150511506
inferFromTypes(target, target);
11506-
priority = savePriority;
11507+
propagationType = savePropagationType;
1150711508
return;
1150811509
}
1150911510
if (source.aliasSymbol && source.aliasTypeArguments && source.aliasSymbol === target.aliasSymbol) {
@@ -11567,16 +11568,13 @@ namespace ts {
1156711568
const inference = getInferenceInfoForType(target);
1156811569
if (inference) {
1156911570
if (!inference.isFixed) {
11570-
const p = priority & InferencePriority.Mask;
11571-
if (inference.priority === undefined || p < inference.priority) {
11571+
if (inference.priority === undefined || priority < inference.priority) {
1157211572
inference.candidates = undefined;
1157311573
inference.contraCandidates = undefined;
11574-
inference.priority = p;
11574+
inference.priority = priority;
1157511575
}
11576-
if (p === inference.priority) {
11577-
const candidate = priority & InferencePriority.Never ? neverType :
11578-
priority & InferencePriority.Wildcard ? wildcardType :
11579-
source;
11576+
if (priority === inference.priority) {
11577+
const candidate = propagationType || source;
1158011578
if (contravariant) {
1158111579
inference.contraCandidates = append(inference.contraCandidates, candidate);
1158211580
}

src/compiler/types.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3915,9 +3915,6 @@ namespace ts {
39153915
ReturnType = 1 << 2, // Inference made from return type of generic function
39163916
NoConstraints = 1 << 3, // Don't infer from constraints of instantiable types
39173917
AlwaysStrict = 1 << 4, // Always use strict rules for contravariant inferences
3918-
Wildcard = 1 << 5, // Inferring from wildcard type
3919-
Never = 1 << 6, // Inferring from never type
3920-
Mask = NakedTypeVariable | MappedType | ReturnType,
39213918
}
39223919

39233920
/* @internal */

0 commit comments

Comments
 (0)