Skip to content

Normalize type references before relating them #35266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14240,6 +14240,14 @@ namespace ts {
return getObjectFlags(source) & ObjectFlags.JsxAttributes && !isUnhyphenatedJsxName(sourceProp.escapedName);
}

function getNormalizedType(type: Type, writing: boolean): Type {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will this function jibe with #31804 and the associated fix #32116 - does this function just become recursive?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to just move the loop into getNormalizedType.

return isFreshLiteralType(type) ? (<FreshableType>type).regularType :
getObjectFlags(type) & ObjectFlags.Reference && (<TypeReference>type).node ? createTypeReference((<TypeReference>type).target, getTypeArguments(<TypeReference>type)) :
type.flags & TypeFlags.Substitution ? writing ? (<SubstitutionType>type).typeVariable : (<SubstitutionType>type).substitute :
type.flags & TypeFlags.Simplifiable ? getSimplifiedType(type, writing) :
type;
}

/**
* Checks if 'source' is related to 'target' (e.g.: is a assignable to).
* @param source The left-hand-side of the relation.
Expand Down Expand Up @@ -14546,24 +14554,12 @@ namespace ts {
* * Ternary.False if they are not related.
*/
function isRelatedTo(source: Type, target: Type, reportErrors = false, headMessage?: DiagnosticMessage, isApparentIntersectionConstituent?: boolean): Ternary {
if (isFreshLiteralType(source)) {
source = (<FreshableType>source).regularType;
}
if (isFreshLiteralType(target)) {
target = (<FreshableType>target).regularType;
}
if (source.flags & TypeFlags.Substitution) {
source = (<SubstitutionType>source).substitute;
}
if (target.flags & TypeFlags.Substitution) {
target = (<SubstitutionType>target).typeVariable;
}
if (source.flags & TypeFlags.Simplifiable) {
source = getSimplifiedType(source, /*writing*/ false);
}
if (target.flags & TypeFlags.Simplifiable) {
target = getSimplifiedType(target, /*writing*/ true);
}
// Normalize the source and target types: Turn fresh literal types into regular literal types,
// turn deferred type references into regular type references, simplify indexed access and
// conditional types, and resolve substitution types to either the substitution (on the source
// side) or the type variable (on the target side).
source = getNormalizedType(source, /*writing*/ false);
target = getNormalizedType(target, /*writing*/ true);

// Try to see if we're relating something like `Foo` -> `Bar | null | undefined`.
// If so, reporting the `null` and `undefined` in the type is hardly useful.
Expand Down
24 changes: 12 additions & 12 deletions tests/baselines/reference/optionalTupleElements1.errors.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
tests/cases/conformance/types/tuple/optionalTupleElements1.ts(11,29): error TS1257: A required element cannot follow an optional element.
tests/cases/conformance/types/tuple/optionalTupleElements1.ts(15,5): error TS2322: Type 'T2' is not assignable to type 'T1'.
tests/cases/conformance/types/tuple/optionalTupleElements1.ts(15,5): error TS2322: Type '[number, string, (boolean | undefined)?]' is not assignable to type '[number, string, boolean]'.
Types of property '2' are incompatible.
Type 'boolean | undefined' is not assignable to type 'boolean'.
Type 'undefined' is not assignable to type 'boolean'.
tests/cases/conformance/types/tuple/optionalTupleElements1.ts(16,5): error TS2322: Type 'T3' is not assignable to type 'T1'.
tests/cases/conformance/types/tuple/optionalTupleElements1.ts(16,5): error TS2322: Type '[number, (string | undefined)?, (boolean | undefined)?]' is not assignable to type '[number, string, boolean]'.
Types of property '1' are incompatible.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
tests/cases/conformance/types/tuple/optionalTupleElements1.ts(17,5): error TS2322: Type 'T4' is not assignable to type 'T1'.
tests/cases/conformance/types/tuple/optionalTupleElements1.ts(17,5): error TS2322: Type '[(number | undefined)?, (string | undefined)?, (boolean | undefined)?]' is not assignable to type '[number, string, boolean]'.
Types of property '0' are incompatible.
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.
tests/cases/conformance/types/tuple/optionalTupleElements1.ts(20,5): error TS2322: Type 'T3' is not assignable to type 'T2'.
tests/cases/conformance/types/tuple/optionalTupleElements1.ts(20,5): error TS2322: Type '[number, (string | undefined)?, (boolean | undefined)?]' is not assignable to type '[number, string, (boolean | undefined)?]'.
Types of property '1' are incompatible.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
tests/cases/conformance/types/tuple/optionalTupleElements1.ts(21,5): error TS2322: Type 'T4' is not assignable to type 'T2'.
tests/cases/conformance/types/tuple/optionalTupleElements1.ts(21,5): error TS2322: Type '[(number | undefined)?, (string | undefined)?, (boolean | undefined)?]' is not assignable to type '[number, string, (boolean | undefined)?]'.
Types of property '0' are incompatible.
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.
tests/cases/conformance/types/tuple/optionalTupleElements1.ts(25,5): error TS2322: Type 'T4' is not assignable to type 'T3'.
tests/cases/conformance/types/tuple/optionalTupleElements1.ts(25,5): error TS2322: Type '[(number | undefined)?, (string | undefined)?, (boolean | undefined)?]' is not assignable to type '[number, (string | undefined)?, (boolean | undefined)?]'.
Types of property '0' are incompatible.
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.
Expand All @@ -44,33 +44,33 @@ tests/cases/conformance/types/tuple/optionalTupleElements1.ts(25,5): error TS232
t1 = t1;
t1 = t2; // Error
~~
!!! error TS2322: Type 'T2' is not assignable to type 'T1'.
!!! error TS2322: Type '[number, string, (boolean | undefined)?]' is not assignable to type '[number, string, boolean]'.
!!! error TS2322: Types of property '2' are incompatible.
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
!!! error TS2322: Type 'undefined' is not assignable to type 'boolean'.
t1 = t3; // Error
~~
!!! error TS2322: Type 'T3' is not assignable to type 'T1'.
!!! error TS2322: Type '[number, (string | undefined)?, (boolean | undefined)?]' is not assignable to type '[number, string, boolean]'.
!!! error TS2322: Types of property '1' are incompatible.
!!! error TS2322: Type 'string | undefined' is not assignable to type 'string'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
t1 = t4; // Error
~~
!!! error TS2322: Type 'T4' is not assignable to type 'T1'.
!!! error TS2322: Type '[(number | undefined)?, (string | undefined)?, (boolean | undefined)?]' is not assignable to type '[number, string, boolean]'.
!!! error TS2322: Types of property '0' are incompatible.
!!! error TS2322: Type 'number | undefined' is not assignable to type 'number'.
!!! error TS2322: Type 'undefined' is not assignable to type 'number'.
t2 = t1;
t2 = t2;
t2 = t3; // Error
~~
!!! error TS2322: Type 'T3' is not assignable to type 'T2'.
!!! error TS2322: Type '[number, (string | undefined)?, (boolean | undefined)?]' is not assignable to type '[number, string, (boolean | undefined)?]'.
!!! error TS2322: Types of property '1' are incompatible.
!!! error TS2322: Type 'string | undefined' is not assignable to type 'string'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
t2 = t4; // Error
~~
!!! error TS2322: Type 'T4' is not assignable to type 'T2'.
!!! error TS2322: Type '[(number | undefined)?, (string | undefined)?, (boolean | undefined)?]' is not assignable to type '[number, string, (boolean | undefined)?]'.
!!! error TS2322: Types of property '0' are incompatible.
!!! error TS2322: Type 'number | undefined' is not assignable to type 'number'.
!!! error TS2322: Type 'undefined' is not assignable to type 'number'.
Expand All @@ -79,7 +79,7 @@ tests/cases/conformance/types/tuple/optionalTupleElements1.ts(25,5): error TS232
t3 = t3;
t3 = t4; // Error
~~
!!! error TS2322: Type 'T4' is not assignable to type 'T3'.
!!! error TS2322: Type '[(number | undefined)?, (string | undefined)?, (boolean | undefined)?]' is not assignable to type '[number, (string | undefined)?, (boolean | undefined)?]'.
!!! error TS2322: Types of property '0' are incompatible.
!!! error TS2322: Type 'number | undefined' is not assignable to type 'number'.
!!! error TS2322: Type 'undefined' is not assignable to type 'number'.
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/recursiveTypeReferences1.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/conformance/types/typeRelationships/recursiveTypes/recursiveTypeReferences1.ts(45,7): error TS2322: Type '42' is not assignable to type 'Box2'.
tests/cases/conformance/types/typeRelationships/recursiveTypes/recursiveTypeReferences1.ts(45,7): error TS2322: Type '42' is not assignable to type 'Box<number | Box2>'.
tests/cases/conformance/types/typeRelationships/recursiveTypes/recursiveTypeReferences1.ts(60,7): error TS2322: Type 'number' is not assignable to type 'string | RecArray<string>'.
tests/cases/conformance/types/typeRelationships/recursiveTypes/recursiveTypeReferences1.ts(66,8): error TS2322: Type 'number' is not assignable to type 'string | string[]'.
tests/cases/conformance/types/typeRelationships/recursiveTypes/recursiveTypeReferences1.ts(72,8): error TS2322: Type 'number' is not assignable to type 'string | (string | string[])[]'.
Expand Down Expand Up @@ -60,7 +60,7 @@ tests/cases/conformance/types/typeRelationships/recursiveTypes/recursiveTypeRefe

const b20: Box2 = 42; // Error
~~~
!!! error TS2322: Type '42' is not assignable to type 'Box2'.
!!! error TS2322: Type '42' is not assignable to type 'Box<number | Box2>'.
Copy link
Member

@weswigham weswigham Nov 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😦 Is it possible we want to keep the pre-normalized form around for error reporting? Since it might have a nicer alias for us to use?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at doing that, but I'm not sure it really is all that valuable. We still display the alias in all other situations, it's only when we're elaborating into the type that we now display structure one level earlier that we would before. I think the few places it affects are just as well served with the change in this PR (which, BTW, is what we used to always do).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, but we acknowledged that these errors were better - a nice side effect of the recursive alias change. it would be nice if we could keep them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, changed the code to preserve the original type and use it in error reporting if it has an alias.

const b21: Box2 = { value: 42 };
const b22: Box2 = { value: { value: { value: 42 }}};

Expand Down
35 changes: 35 additions & 0 deletions tests/baselines/reference/unwitnessedTypeParameterVariance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//// [unwitnessedTypeParameterVariance.ts]
// Repros from #33872

export interface CalcObj<O> {
read: (origin: O) => CalcValue<O>;
}

export type CalcValue<O> = CalcObj<O>;

function foo<O>() {
const unk: CalcObj<unknown> = { read: (origin: unknown) => unk }
const x: CalcObj<O> = unk;
}

type A<T> = B<T>;

interface B<T> {
prop: A<T>;
}

declare let a: A<number>;
declare let b: A<3>;

b = a;


//// [unwitnessedTypeParameterVariance.js]
"use strict";
// Repros from #33872
exports.__esModule = true;
function foo() {
var unk = { read: function (origin) { return unk; } };
var x = unk;
}
b = a;
67 changes: 67 additions & 0 deletions tests/baselines/reference/unwitnessedTypeParameterVariance.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
=== tests/cases/compiler/unwitnessedTypeParameterVariance.ts ===
// Repros from #33872

export interface CalcObj<O> {
>CalcObj : Symbol(CalcObj, Decl(unwitnessedTypeParameterVariance.ts, 0, 0))
>O : Symbol(O, Decl(unwitnessedTypeParameterVariance.ts, 2, 25))

read: (origin: O) => CalcValue<O>;
>read : Symbol(CalcObj.read, Decl(unwitnessedTypeParameterVariance.ts, 2, 29))
>origin : Symbol(origin, Decl(unwitnessedTypeParameterVariance.ts, 3, 11))
>O : Symbol(O, Decl(unwitnessedTypeParameterVariance.ts, 2, 25))
>CalcValue : Symbol(CalcValue, Decl(unwitnessedTypeParameterVariance.ts, 4, 1))
>O : Symbol(O, Decl(unwitnessedTypeParameterVariance.ts, 2, 25))
}

export type CalcValue<O> = CalcObj<O>;
>CalcValue : Symbol(CalcValue, Decl(unwitnessedTypeParameterVariance.ts, 4, 1))
>O : Symbol(O, Decl(unwitnessedTypeParameterVariance.ts, 6, 22))
>CalcObj : Symbol(CalcObj, Decl(unwitnessedTypeParameterVariance.ts, 0, 0))
>O : Symbol(O, Decl(unwitnessedTypeParameterVariance.ts, 6, 22))

function foo<O>() {
>foo : Symbol(foo, Decl(unwitnessedTypeParameterVariance.ts, 6, 38))
>O : Symbol(O, Decl(unwitnessedTypeParameterVariance.ts, 8, 13))

const unk: CalcObj<unknown> = { read: (origin: unknown) => unk }
>unk : Symbol(unk, Decl(unwitnessedTypeParameterVariance.ts, 9, 9))
>CalcObj : Symbol(CalcObj, Decl(unwitnessedTypeParameterVariance.ts, 0, 0))
>read : Symbol(read, Decl(unwitnessedTypeParameterVariance.ts, 9, 35))
>origin : Symbol(origin, Decl(unwitnessedTypeParameterVariance.ts, 9, 43))
>unk : Symbol(unk, Decl(unwitnessedTypeParameterVariance.ts, 9, 9))

const x: CalcObj<O> = unk;
>x : Symbol(x, Decl(unwitnessedTypeParameterVariance.ts, 10, 9))
>CalcObj : Symbol(CalcObj, Decl(unwitnessedTypeParameterVariance.ts, 0, 0))
>O : Symbol(O, Decl(unwitnessedTypeParameterVariance.ts, 8, 13))
>unk : Symbol(unk, Decl(unwitnessedTypeParameterVariance.ts, 9, 9))
}

type A<T> = B<T>;
>A : Symbol(A, Decl(unwitnessedTypeParameterVariance.ts, 11, 1))
>T : Symbol(T, Decl(unwitnessedTypeParameterVariance.ts, 13, 7))
>B : Symbol(B, Decl(unwitnessedTypeParameterVariance.ts, 13, 17))
>T : Symbol(T, Decl(unwitnessedTypeParameterVariance.ts, 13, 7))

interface B<T> {
>B : Symbol(B, Decl(unwitnessedTypeParameterVariance.ts, 13, 17))
>T : Symbol(T, Decl(unwitnessedTypeParameterVariance.ts, 15, 12))

prop: A<T>;
>prop : Symbol(B.prop, Decl(unwitnessedTypeParameterVariance.ts, 15, 16))
>A : Symbol(A, Decl(unwitnessedTypeParameterVariance.ts, 11, 1))
>T : Symbol(T, Decl(unwitnessedTypeParameterVariance.ts, 15, 12))
}

declare let a: A<number>;
>a : Symbol(a, Decl(unwitnessedTypeParameterVariance.ts, 19, 11))
>A : Symbol(A, Decl(unwitnessedTypeParameterVariance.ts, 11, 1))

declare let b: A<3>;
>b : Symbol(b, Decl(unwitnessedTypeParameterVariance.ts, 20, 11))
>A : Symbol(A, Decl(unwitnessedTypeParameterVariance.ts, 11, 1))

b = a;
>b : Symbol(b, Decl(unwitnessedTypeParameterVariance.ts, 20, 11))
>a : Symbol(a, Decl(unwitnessedTypeParameterVariance.ts, 19, 11))

47 changes: 47 additions & 0 deletions tests/baselines/reference/unwitnessedTypeParameterVariance.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
=== tests/cases/compiler/unwitnessedTypeParameterVariance.ts ===
// Repros from #33872

export interface CalcObj<O> {
read: (origin: O) => CalcValue<O>;
>read : (origin: O) => CalcValue<O>
>origin : O
}

export type CalcValue<O> = CalcObj<O>;
>CalcValue : CalcValue<O>

function foo<O>() {
>foo : <O>() => void

const unk: CalcObj<unknown> = { read: (origin: unknown) => unk }
>unk : CalcObj<unknown>
>{ read: (origin: unknown) => unk } : { read: (origin: unknown) => CalcObj<unknown>; }
>read : (origin: unknown) => CalcObj<unknown>
>(origin: unknown) => unk : (origin: unknown) => CalcObj<unknown>
>origin : unknown
>unk : CalcObj<unknown>

const x: CalcObj<O> = unk;
>x : CalcObj<O>
>unk : CalcObj<unknown>
}

type A<T> = B<T>;
>A : A<T>

interface B<T> {
prop: A<T>;
>prop : A<T>
}

declare let a: A<number>;
>a : A<number>

declare let b: A<3>;
>b : A<3>

b = a;
>b = a : A<number>
>b : A<3>
>a : A<number>

25 changes: 25 additions & 0 deletions tests/cases/compiler/unwitnessedTypeParameterVariance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// @strict: true

// Repros from #33872

export interface CalcObj<O> {
read: (origin: O) => CalcValue<O>;
}

export type CalcValue<O> = CalcObj<O>;

function foo<O>() {
const unk: CalcObj<unknown> = { read: (origin: unknown) => unk }
const x: CalcObj<O> = unk;
}

type A<T> = B<T>;

interface B<T> {
prop: A<T>;
}

declare let a: A<number>;
declare let b: A<3>;

b = a;