Skip to content

Commit 46aa15c

Browse files
committed
Introduce InferencePriority.EmptyLiteral
1 parent 344e571 commit 46aa15c

19 files changed

+62
-45
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25237,6 +25237,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2523725237
if (candidate === blockedStringType) {
2523825238
return;
2523925239
}
25240+
if (isEmptyLiteralType(candidate) || isEmptyArrayLiteralType(candidate)) {
25241+
priority |= InferencePriority.EmptyLiteral;
25242+
}
2524025243
if (inference.priority === undefined || priority < inference.priority) {
2524125244
inference.candidates = undefined;
2524225245
inference.contraCandidates = undefined;
@@ -25836,18 +25839,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2583625839
applyToReturnTypes(source, target, inferFromTypes);
2583725840
}
2583825841

25839-
function hasPrimitiveMappedTypeParameterConstaint(mappedType: MappedType): boolean {
25840-
const constraint = mappedType.typeParameter && getConstraintOfTypeParameter(mappedType.typeParameter);
25841-
return !!(constraint && constraint.flags & TypeFlags.Primitive);
25842-
}
25843-
2584425842
function inferFromIndexTypes(source: Type, target: Type) {
2584525843
// Inferences across mapped type index signatures are pretty much the same a inferences to homomorphic variables
25846-
const priority = (getObjectFlags(source) & getObjectFlags(target) & ObjectFlags.Mapped) &&
25847-
!hasPrimitiveMappedTypeParameterConstaint(source as MappedType) &&
25848-
!hasPrimitiveMappedTypeParameterConstaint(target as MappedType) ?
25849-
InferencePriority.HomomorphicMappedType :
25850-
InferencePriority.None;
25844+
const priority = (getObjectFlags(source) & getObjectFlags(target) & ObjectFlags.Mapped) ? InferencePriority.HomomorphicMappedType : 0;
2585125845
const indexInfos = getIndexInfosOfType(target);
2585225846
if (isObjectTypeWithInferableIndex(source)) {
2585325847
for (const targetInfo of indexInfos) {

src/compiler/types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6819,9 +6819,10 @@ export const enum InferencePriority {
68196819
ContravariantConditional = 1 << 6, // Conditional type in contravariant position
68206820
ReturnType = 1 << 7, // Inference made from return type of generic function
68216821
LiteralKeyof = 1 << 8, // Inference made from a string literal to a keyof T
6822-
NoConstraints = 1 << 9, // Don't infer from constraints of instantiable types
6823-
AlwaysStrict = 1 << 10, // Always use strict rules for contravariant inferences
6824-
MaxValue = 1 << 11, // Seed for inference priority tracking
6822+
EmptyLiteral = 1 << 9, // Inference made from an empty literal type
6823+
NoConstraints = 1 << 10, // Don't infer from constraints of instantiable types
6824+
AlwaysStrict = 1 << 11, // Always use strict rules for contravariant inferences
6825+
MaxValue = 1 << 12, // Seed for inference priority tracking
68256826

68266827
PriorityImpliesCombination = ReturnType | MappedTypeConstraint | LiteralKeyof, // These priorities imply that the resulting type should be a combination of all candidates
68276828
Circularity = -1, // Inference circularity (value less than all other priorities)

tests/baselines/reference/api/typescript.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7362,9 +7362,10 @@ declare namespace ts {
73627362
ContravariantConditional = 64,
73637363
ReturnType = 128,
73647364
LiteralKeyof = 256,
7365-
NoConstraints = 512,
7366-
AlwaysStrict = 1024,
7367-
MaxValue = 2048,
7365+
EmptyLiteral = 512,
7366+
NoConstraints = 1024,
7367+
AlwaysStrict = 2048,
7368+
MaxValue = 4096,
73687369
PriorityImpliesCombination = 416,
73697370
Circularity = -1,
73707371
}

tests/baselines/reference/taggedTemplateStringsTypeArgumentInference.errors.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
taggedTemplateStringsTypeArgumentInference.ts(62,36): error TS2345: Argument of type '0' is not assignable to parameter of type '""'.
22
taggedTemplateStringsTypeArgumentInference.ts(63,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'a9a' must be of type 'string', but here has type '{}'.
33
taggedTemplateStringsTypeArgumentInference.ts(76,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'a9e' must be of type '{ x: number; z: Date; y?: undefined; } | { x: number; y: string; z?: undefined; }', but here has type '{}'.
4+
taggedTemplateStringsTypeArgumentInference.ts(89,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'arr' must be of type 'any', but here has type 'any[]'.
45

56

6-
==== taggedTemplateStringsTypeArgumentInference.ts (3 errors) ====
7+
==== taggedTemplateStringsTypeArgumentInference.ts (4 errors) ====
78
// Generic tag with one parameter
89
function noParams<T>(n: T) { }
910
noParams ``;
@@ -101,5 +102,8 @@ taggedTemplateStringsTypeArgumentInference.ts(76,5): error TS2403: Subsequent va
101102
// Generic tag with multiple parameters of generic type where one argument is [] and the other is not 'any'
102103
var arr = someGenerics9 `${ [] }${ null }${ undefined }`;
103104
var arr: any[];
105+
~~~
106+
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'arr' must be of type 'any', but here has type 'any[]'.
107+
!!! related TS6203 taggedTemplateStringsTypeArgumentInference.ts:88:5: 'arr' was also declared here.
104108

105109

tests/baselines/reference/taggedTemplateStringsTypeArgumentInference.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,14 +380,14 @@ var a: any;
380380

381381
// Generic tag with multiple parameters of generic type where one argument is [] and the other is not 'any'
382382
var arr = someGenerics9 `${ [] }${ null }${ undefined }`;
383-
>arr : any[]
384-
>someGenerics9 `${ [] }${ null }${ undefined }` : any[]
383+
>arr : any
384+
>someGenerics9 `${ [] }${ null }${ undefined }` : any
385385
>someGenerics9 : <T>(strs: TemplateStringsArray, a: T, b: T, c: T) => T
386386
>`${ [] }${ null }${ undefined }` : string
387387
>[] : undefined[]
388388
>undefined : undefined
389389

390390
var arr: any[];
391-
>arr : any[]
391+
>arr : any
392392

393393

tests/baselines/reference/taggedTemplateStringsTypeArgumentInferenceES6.errors.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
taggedTemplateStringsTypeArgumentInferenceES6.ts(62,36): error TS2345: Argument of type '0' is not assignable to parameter of type '""'.
22
taggedTemplateStringsTypeArgumentInferenceES6.ts(63,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'a9a' must be of type 'string', but here has type '{}'.
33
taggedTemplateStringsTypeArgumentInferenceES6.ts(76,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'a9e' must be of type '{ x: number; z: Date; y?: undefined; } | { x: number; y: string; z?: undefined; }', but here has type '{}'.
4+
taggedTemplateStringsTypeArgumentInferenceES6.ts(89,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'arr' must be of type 'any', but here has type 'any[]'.
45

56

6-
==== taggedTemplateStringsTypeArgumentInferenceES6.ts (3 errors) ====
7+
==== taggedTemplateStringsTypeArgumentInferenceES6.ts (4 errors) ====
78
// Generic tag with one parameter
89
function noParams<T>(n: T) { }
910
noParams ``;
@@ -101,5 +102,8 @@ taggedTemplateStringsTypeArgumentInferenceES6.ts(76,5): error TS2403: Subsequent
101102
// Generic tag with multiple parameters of generic type where one argument is [] and the other is not 'any'
102103
var arr = someGenerics9 `${ [] }${ null }${ undefined }`;
103104
var arr: any[];
105+
~~~
106+
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'arr' must be of type 'any', but here has type 'any[]'.
107+
!!! related TS6203 taggedTemplateStringsTypeArgumentInferenceES6.ts:88:5: 'arr' was also declared here.
104108

105109

tests/baselines/reference/taggedTemplateStringsTypeArgumentInferenceES6.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,14 +380,14 @@ var a: any;
380380

381381
// Generic tag with multiple parameters of generic type where one argument is [] and the other is not 'any'
382382
var arr = someGenerics9 `${ [] }${ null }${ undefined }`;
383-
>arr : any[]
384-
>someGenerics9 `${ [] }${ null }${ undefined }` : any[]
383+
>arr : any
384+
>someGenerics9 `${ [] }${ null }${ undefined }` : any
385385
>someGenerics9 : <T>(strs: TemplateStringsArray, a: T, b: T, c: T) => T
386386
>`${ [] }${ null }${ undefined }` : string
387387
>[] : undefined[]
388388
>undefined : undefined
389389

390390
var arr: any[];
391-
>arr : any[]
391+
>arr : any
392392

393393

tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function fn2() { return undefined; }
4747

4848
var d1: Date = fn2 `${ 0 }${ undefined }`; // contextually typed
4949
>d1 : Date
50-
>fn2 `${ 0 }${ undefined }` : any
50+
>fn2 `${ 0 }${ undefined }` : Date
5151
>fn2 : { (strs: TemplateStringsArray, s: string, n: number): number; <T>(strs: TemplateStringsArray, n: number, t: T): T; }
5252
>`${ 0 }${ undefined }` : string
5353
>0 : 0

tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3_ES6.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function fn2() { return undefined; }
4747

4848
var d1: Date = fn2 `${ 0 }${ undefined }`; // contextually typed
4949
>d1 : Date
50-
>fn2 `${ 0 }${ undefined }` : any
50+
>fn2 `${ 0 }${ undefined }` : Date
5151
>fn2 : { (strs: TemplateStringsArray, s: string, n: number): number; <T>(strs: TemplateStringsArray, n: number, t: T): T; }
5252
>`${ 0 }${ undefined }` : string
5353
>0 : 0

tests/baselines/reference/tupleTypes.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ var tt2: number | string;
155155
>tt2 : undefined
156156

157157
tt = tuple2(1, undefined);
158-
>tt = tuple2(1, undefined) : [number, any]
158+
>tt = tuple2(1, undefined) : [number, string]
159159
>tt : [number, string]
160-
>tuple2(1, undefined) : [number, any]
160+
>tuple2(1, undefined) : [number, string]
161161
>tuple2 : <T0, T1>(item0: T0, item1: T1) => [T0, T1]
162162
>1 : 1
163163
>undefined : undefined

0 commit comments

Comments
 (0)