Skip to content

Commit 15dd000

Browse files
authored
Unwrap substitutions both before _and_ after potential simplification (#32116)
* Unwrap substitutions both before _and_ after potential simplification * Repeatedly unwrap/simplify until no more can be performed * Use seperate loops for source and target to reduce redundant calls * Move loop into function * Inline worker
1 parent 4d5464e commit 15dd000

5 files changed

+183
-5
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14826,11 +14826,16 @@ namespace ts {
1482614826
}
1482714827

1482814828
function getNormalizedType(type: Type, writing: boolean): Type {
14829-
return isFreshLiteralType(type) ? (<FreshableType>type).regularType :
14830-
getObjectFlags(type) & ObjectFlags.Reference && (<TypeReference>type).node ? createTypeReference((<TypeReference>type).target, getTypeArguments(<TypeReference>type)) :
14831-
type.flags & TypeFlags.Substitution ? writing ? (<SubstitutionType>type).typeVariable : (<SubstitutionType>type).substitute :
14832-
type.flags & TypeFlags.Simplifiable ? getSimplifiedType(type, writing) :
14833-
type;
14829+
do {
14830+
const t = isFreshLiteralType(type) ? (<FreshableType>type).regularType :
14831+
getObjectFlags(type) & ObjectFlags.Reference && (<TypeReference>type).node ? createTypeReference((<TypeReference>type).target, getTypeArguments(<TypeReference>type)) :
14832+
type.flags & TypeFlags.Substitution ? writing ? (<SubstitutionType>type).typeVariable : (<SubstitutionType>type).substitute :
14833+
type.flags & TypeFlags.Simplifiable ? getSimplifiedType(type, writing) :
14834+
type;
14835+
if (t === type) break;
14836+
type = t;
14837+
} while (true);
14838+
return type;
1483414839
}
1483514840

1483614841
/**
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//// [indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts]
2+
type AnyFunction = (...args: any[]) => any;
3+
type Params<T> = Parameters<Extract<T, AnyFunction>>;
4+
5+
interface Wrapper<T> {
6+
call<K extends keyof T>(event: K, ...args: Params<T[K]>): void;
7+
}
8+
9+
interface AWrapped {
10+
foo(): void;
11+
}
12+
13+
class A {
14+
foo: Wrapper<AWrapped>;
15+
}
16+
17+
interface BWrapped extends AWrapped {
18+
bar(): void;
19+
}
20+
21+
class B extends A {
22+
foo: Wrapper<BWrapped>;
23+
}
24+
25+
//// [indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.js]
26+
var __extends = (this && this.__extends) || (function () {
27+
var extendStatics = function (d, b) {
28+
extendStatics = Object.setPrototypeOf ||
29+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
30+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
31+
return extendStatics(d, b);
32+
};
33+
return function (d, b) {
34+
extendStatics(d, b);
35+
function __() { this.constructor = d; }
36+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
37+
};
38+
})();
39+
var A = /** @class */ (function () {
40+
function A() {
41+
}
42+
return A;
43+
}());
44+
var B = /** @class */ (function (_super) {
45+
__extends(B, _super);
46+
function B() {
47+
return _super !== null && _super.apply(this, arguments) || this;
48+
}
49+
return B;
50+
}(A));
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
=== tests/cases/compiler/indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts ===
2+
type AnyFunction = (...args: any[]) => any;
3+
>AnyFunction : Symbol(AnyFunction, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 0, 0))
4+
>args : Symbol(args, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 0, 20))
5+
6+
type Params<T> = Parameters<Extract<T, AnyFunction>>;
7+
>Params : Symbol(Params, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 0, 43))
8+
>T : Symbol(T, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 1, 12))
9+
>Parameters : Symbol(Parameters, Decl(lib.es5.d.ts, --, --))
10+
>Extract : Symbol(Extract, Decl(lib.es5.d.ts, --, --))
11+
>T : Symbol(T, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 1, 12))
12+
>AnyFunction : Symbol(AnyFunction, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 0, 0))
13+
14+
interface Wrapper<T> {
15+
>Wrapper : Symbol(Wrapper, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 1, 53))
16+
>T : Symbol(T, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 3, 18))
17+
18+
call<K extends keyof T>(event: K, ...args: Params<T[K]>): void;
19+
>call : Symbol(Wrapper.call, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 3, 22))
20+
>K : Symbol(K, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 4, 6))
21+
>T : Symbol(T, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 3, 18))
22+
>event : Symbol(event, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 4, 25))
23+
>K : Symbol(K, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 4, 6))
24+
>args : Symbol(args, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 4, 34))
25+
>Params : Symbol(Params, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 0, 43))
26+
>T : Symbol(T, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 3, 18))
27+
>K : Symbol(K, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 4, 6))
28+
}
29+
30+
interface AWrapped {
31+
>AWrapped : Symbol(AWrapped, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 5, 1))
32+
33+
foo(): void;
34+
>foo : Symbol(AWrapped.foo, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 7, 20))
35+
}
36+
37+
class A {
38+
>A : Symbol(A, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 9, 1))
39+
40+
foo: Wrapper<AWrapped>;
41+
>foo : Symbol(A.foo, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 11, 9))
42+
>Wrapper : Symbol(Wrapper, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 1, 53))
43+
>AWrapped : Symbol(AWrapped, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 5, 1))
44+
}
45+
46+
interface BWrapped extends AWrapped {
47+
>BWrapped : Symbol(BWrapped, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 13, 1))
48+
>AWrapped : Symbol(AWrapped, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 5, 1))
49+
50+
bar(): void;
51+
>bar : Symbol(BWrapped.bar, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 15, 37))
52+
}
53+
54+
class B extends A {
55+
>B : Symbol(B, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 17, 1))
56+
>A : Symbol(A, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 9, 1))
57+
58+
foo: Wrapper<BWrapped>;
59+
>foo : Symbol(B.foo, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 19, 19))
60+
>Wrapper : Symbol(Wrapper, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 1, 53))
61+
>BWrapped : Symbol(BWrapped, Decl(indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts, 13, 1))
62+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
=== tests/cases/compiler/indexedAccessKeyofNestedSimplifiedSubstituteUnwrapped.ts ===
2+
type AnyFunction = (...args: any[]) => any;
3+
>AnyFunction : AnyFunction
4+
>args : any[]
5+
6+
type Params<T> = Parameters<Extract<T, AnyFunction>>;
7+
>Params : Parameters<Extract<T, AnyFunction>>
8+
9+
interface Wrapper<T> {
10+
call<K extends keyof T>(event: K, ...args: Params<T[K]>): void;
11+
>call : <K extends keyof T>(event: K, ...args: Parameters<Extract<T[K], AnyFunction>>) => void
12+
>event : K
13+
>args : Parameters<Extract<T[K], AnyFunction>>
14+
}
15+
16+
interface AWrapped {
17+
foo(): void;
18+
>foo : () => void
19+
}
20+
21+
class A {
22+
>A : A
23+
24+
foo: Wrapper<AWrapped>;
25+
>foo : Wrapper<AWrapped>
26+
}
27+
28+
interface BWrapped extends AWrapped {
29+
bar(): void;
30+
>bar : () => void
31+
}
32+
33+
class B extends A {
34+
>B : B
35+
>A : A
36+
37+
foo: Wrapper<BWrapped>;
38+
>foo : Wrapper<BWrapped>
39+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
type AnyFunction = (...args: any[]) => any;
2+
type Params<T> = Parameters<Extract<T, AnyFunction>>;
3+
4+
interface Wrapper<T> {
5+
call<K extends keyof T>(event: K, ...args: Params<T[K]>): void;
6+
}
7+
8+
interface AWrapped {
9+
foo(): void;
10+
}
11+
12+
class A {
13+
foo: Wrapper<AWrapped>;
14+
}
15+
16+
interface BWrapped extends AWrapped {
17+
bar(): void;
18+
}
19+
20+
class B extends A {
21+
foo: Wrapper<BWrapped>;
22+
}

0 commit comments

Comments
 (0)