Skip to content

Commit 58a05f3

Browse files
authored
Normalize type references before relating them (microsoft#35266)
* Normalize type references before relating them in isRelatedTo * Add comments * Accept new baselines * Add regression tests * Accept new baselines * Use aliases when available in error reporting * Accept new baselines
1 parent 369900b commit 58a05f3

File tree

5 files changed

+191
-19
lines changed

5 files changed

+191
-19
lines changed

src/compiler/checker.ts

+17-19
Original file line numberDiff line numberDiff line change
@@ -14250,6 +14250,14 @@ namespace ts {
1425014250
return getObjectFlags(source) & ObjectFlags.JsxAttributes && !isUnhyphenatedJsxName(sourceProp.escapedName);
1425114251
}
1425214252

14253+
function getNormalizedType(type: Type, writing: boolean): Type {
14254+
return isFreshLiteralType(type) ? (<FreshableType>type).regularType :
14255+
getObjectFlags(type) & ObjectFlags.Reference && (<TypeReference>type).node ? createTypeReference((<TypeReference>type).target, getTypeArguments(<TypeReference>type)) :
14256+
type.flags & TypeFlags.Substitution ? writing ? (<SubstitutionType>type).typeVariable : (<SubstitutionType>type).substitute :
14257+
type.flags & TypeFlags.Simplifiable ? getSimplifiedType(type, writing) :
14258+
type;
14259+
}
14260+
1425314261
/**
1425414262
* Checks if 'source' is related to 'target' (e.g.: is a assignable to).
1425514263
* @param source The left-hand-side of the relation.
@@ -14555,25 +14563,13 @@ namespace ts {
1455514563
* * Ternary.Maybe if they are related with assumptions of other relationships, or
1455614564
* * Ternary.False if they are not related.
1455714565
*/
14558-
function isRelatedTo(source: Type, target: Type, reportErrors = false, headMessage?: DiagnosticMessage, isApparentIntersectionConstituent?: boolean): Ternary {
14559-
if (isFreshLiteralType(source)) {
14560-
source = (<FreshableType>source).regularType;
14561-
}
14562-
if (isFreshLiteralType(target)) {
14563-
target = (<FreshableType>target).regularType;
14564-
}
14565-
if (source.flags & TypeFlags.Substitution) {
14566-
source = (<SubstitutionType>source).substitute;
14567-
}
14568-
if (target.flags & TypeFlags.Substitution) {
14569-
target = (<SubstitutionType>target).typeVariable;
14570-
}
14571-
if (source.flags & TypeFlags.Simplifiable) {
14572-
source = getSimplifiedType(source, /*writing*/ false);
14573-
}
14574-
if (target.flags & TypeFlags.Simplifiable) {
14575-
target = getSimplifiedType(target, /*writing*/ true);
14576-
}
14566+
function isRelatedTo(originalSource: Type, originalTarget: Type, reportErrors = false, headMessage?: DiagnosticMessage, isApparentIntersectionConstituent?: boolean): Ternary {
14567+
// Normalize the source and target types: Turn fresh literal types into regular literal types,
14568+
// turn deferred type references into regular type references, simplify indexed access and
14569+
// conditional types, and resolve substitution types to either the substitution (on the source
14570+
// side) or the type variable (on the target side).
14571+
let source = getNormalizedType(originalSource, /*writing*/ false);
14572+
let target = getNormalizedType(originalTarget, /*writing*/ true);
1457714573

1457814574
// Try to see if we're relating something like `Foo` -> `Bar | null | undefined`.
1457914575
// If so, reporting the `null` and `undefined` in the type is hardly useful.
@@ -14706,6 +14702,8 @@ namespace ts {
1470614702
}
1470714703

1470814704
if (!result && reportErrors) {
14705+
source = originalSource.aliasSymbol ? originalSource : source;
14706+
target = originalTarget.aliasSymbol ? originalTarget : target;
1470914707
let maybeSuppress = overrideNextErrorInfo > 0;
1471014708
if (maybeSuppress) {
1471114709
overrideNextErrorInfo--;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//// [unwitnessedTypeParameterVariance.ts]
2+
// Repros from #33872
3+
4+
export interface CalcObj<O> {
5+
read: (origin: O) => CalcValue<O>;
6+
}
7+
8+
export type CalcValue<O> = CalcObj<O>;
9+
10+
function foo<O>() {
11+
const unk: CalcObj<unknown> = { read: (origin: unknown) => unk }
12+
const x: CalcObj<O> = unk;
13+
}
14+
15+
type A<T> = B<T>;
16+
17+
interface B<T> {
18+
prop: A<T>;
19+
}
20+
21+
declare let a: A<number>;
22+
declare let b: A<3>;
23+
24+
b = a;
25+
26+
27+
//// [unwitnessedTypeParameterVariance.js]
28+
"use strict";
29+
// Repros from #33872
30+
exports.__esModule = true;
31+
function foo() {
32+
var unk = { read: function (origin) { return unk; } };
33+
var x = unk;
34+
}
35+
b = a;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
=== tests/cases/compiler/unwitnessedTypeParameterVariance.ts ===
2+
// Repros from #33872
3+
4+
export interface CalcObj<O> {
5+
>CalcObj : Symbol(CalcObj, Decl(unwitnessedTypeParameterVariance.ts, 0, 0))
6+
>O : Symbol(O, Decl(unwitnessedTypeParameterVariance.ts, 2, 25))
7+
8+
read: (origin: O) => CalcValue<O>;
9+
>read : Symbol(CalcObj.read, Decl(unwitnessedTypeParameterVariance.ts, 2, 29))
10+
>origin : Symbol(origin, Decl(unwitnessedTypeParameterVariance.ts, 3, 11))
11+
>O : Symbol(O, Decl(unwitnessedTypeParameterVariance.ts, 2, 25))
12+
>CalcValue : Symbol(CalcValue, Decl(unwitnessedTypeParameterVariance.ts, 4, 1))
13+
>O : Symbol(O, Decl(unwitnessedTypeParameterVariance.ts, 2, 25))
14+
}
15+
16+
export type CalcValue<O> = CalcObj<O>;
17+
>CalcValue : Symbol(CalcValue, Decl(unwitnessedTypeParameterVariance.ts, 4, 1))
18+
>O : Symbol(O, Decl(unwitnessedTypeParameterVariance.ts, 6, 22))
19+
>CalcObj : Symbol(CalcObj, Decl(unwitnessedTypeParameterVariance.ts, 0, 0))
20+
>O : Symbol(O, Decl(unwitnessedTypeParameterVariance.ts, 6, 22))
21+
22+
function foo<O>() {
23+
>foo : Symbol(foo, Decl(unwitnessedTypeParameterVariance.ts, 6, 38))
24+
>O : Symbol(O, Decl(unwitnessedTypeParameterVariance.ts, 8, 13))
25+
26+
const unk: CalcObj<unknown> = { read: (origin: unknown) => unk }
27+
>unk : Symbol(unk, Decl(unwitnessedTypeParameterVariance.ts, 9, 9))
28+
>CalcObj : Symbol(CalcObj, Decl(unwitnessedTypeParameterVariance.ts, 0, 0))
29+
>read : Symbol(read, Decl(unwitnessedTypeParameterVariance.ts, 9, 35))
30+
>origin : Symbol(origin, Decl(unwitnessedTypeParameterVariance.ts, 9, 43))
31+
>unk : Symbol(unk, Decl(unwitnessedTypeParameterVariance.ts, 9, 9))
32+
33+
const x: CalcObj<O> = unk;
34+
>x : Symbol(x, Decl(unwitnessedTypeParameterVariance.ts, 10, 9))
35+
>CalcObj : Symbol(CalcObj, Decl(unwitnessedTypeParameterVariance.ts, 0, 0))
36+
>O : Symbol(O, Decl(unwitnessedTypeParameterVariance.ts, 8, 13))
37+
>unk : Symbol(unk, Decl(unwitnessedTypeParameterVariance.ts, 9, 9))
38+
}
39+
40+
type A<T> = B<T>;
41+
>A : Symbol(A, Decl(unwitnessedTypeParameterVariance.ts, 11, 1))
42+
>T : Symbol(T, Decl(unwitnessedTypeParameterVariance.ts, 13, 7))
43+
>B : Symbol(B, Decl(unwitnessedTypeParameterVariance.ts, 13, 17))
44+
>T : Symbol(T, Decl(unwitnessedTypeParameterVariance.ts, 13, 7))
45+
46+
interface B<T> {
47+
>B : Symbol(B, Decl(unwitnessedTypeParameterVariance.ts, 13, 17))
48+
>T : Symbol(T, Decl(unwitnessedTypeParameterVariance.ts, 15, 12))
49+
50+
prop: A<T>;
51+
>prop : Symbol(B.prop, Decl(unwitnessedTypeParameterVariance.ts, 15, 16))
52+
>A : Symbol(A, Decl(unwitnessedTypeParameterVariance.ts, 11, 1))
53+
>T : Symbol(T, Decl(unwitnessedTypeParameterVariance.ts, 15, 12))
54+
}
55+
56+
declare let a: A<number>;
57+
>a : Symbol(a, Decl(unwitnessedTypeParameterVariance.ts, 19, 11))
58+
>A : Symbol(A, Decl(unwitnessedTypeParameterVariance.ts, 11, 1))
59+
60+
declare let b: A<3>;
61+
>b : Symbol(b, Decl(unwitnessedTypeParameterVariance.ts, 20, 11))
62+
>A : Symbol(A, Decl(unwitnessedTypeParameterVariance.ts, 11, 1))
63+
64+
b = a;
65+
>b : Symbol(b, Decl(unwitnessedTypeParameterVariance.ts, 20, 11))
66+
>a : Symbol(a, Decl(unwitnessedTypeParameterVariance.ts, 19, 11))
67+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
=== tests/cases/compiler/unwitnessedTypeParameterVariance.ts ===
2+
// Repros from #33872
3+
4+
export interface CalcObj<O> {
5+
read: (origin: O) => CalcValue<O>;
6+
>read : (origin: O) => CalcValue<O>
7+
>origin : O
8+
}
9+
10+
export type CalcValue<O> = CalcObj<O>;
11+
>CalcValue : CalcValue<O>
12+
13+
function foo<O>() {
14+
>foo : <O>() => void
15+
16+
const unk: CalcObj<unknown> = { read: (origin: unknown) => unk }
17+
>unk : CalcObj<unknown>
18+
>{ read: (origin: unknown) => unk } : { read: (origin: unknown) => CalcObj<unknown>; }
19+
>read : (origin: unknown) => CalcObj<unknown>
20+
>(origin: unknown) => unk : (origin: unknown) => CalcObj<unknown>
21+
>origin : unknown
22+
>unk : CalcObj<unknown>
23+
24+
const x: CalcObj<O> = unk;
25+
>x : CalcObj<O>
26+
>unk : CalcObj<unknown>
27+
}
28+
29+
type A<T> = B<T>;
30+
>A : A<T>
31+
32+
interface B<T> {
33+
prop: A<T>;
34+
>prop : A<T>
35+
}
36+
37+
declare let a: A<number>;
38+
>a : A<number>
39+
40+
declare let b: A<3>;
41+
>b : A<3>
42+
43+
b = a;
44+
>b = a : A<number>
45+
>b : A<3>
46+
>a : A<number>
47+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// @strict: true
2+
3+
// Repros from #33872
4+
5+
export interface CalcObj<O> {
6+
read: (origin: O) => CalcValue<O>;
7+
}
8+
9+
export type CalcValue<O> = CalcObj<O>;
10+
11+
function foo<O>() {
12+
const unk: CalcObj<unknown> = { read: (origin: unknown) => unk }
13+
const x: CalcObj<O> = unk;
14+
}
15+
16+
type A<T> = B<T>;
17+
18+
interface B<T> {
19+
prop: A<T>;
20+
}
21+
22+
declare let a: A<number>;
23+
declare let b: A<3>;
24+
25+
b = a;

0 commit comments

Comments
 (0)