Skip to content

Commit 6c058d4

Browse files
authored
Properly account for this argument in intersection apparent type caching (#58677)
1 parent 3aaa614 commit 6c058d4

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

src/compiler/checker.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -14716,7 +14716,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1471614716
}
1471714717

1471814718
function getApparentTypeOfIntersectionType(type: IntersectionType, thisArgument: Type) {
14719-
return type.resolvedApparentType || (type.resolvedApparentType = getTypeWithThisArgument(type, thisArgument, /*needApparentType*/ true));
14719+
if (type === thisArgument) {
14720+
return type.resolvedApparentType || (type.resolvedApparentType = getTypeWithThisArgument(type, thisArgument, /*needApparentType*/ true));
14721+
}
14722+
const key = `I${getTypeId(type)},${getTypeId(thisArgument)}`;
14723+
return getCachedType(key) ?? setCachedType(key, getTypeWithThisArgument(type, thisArgument, /*needApparentType*/ true));
1472014724
}
1472114725

1472214726
function getResolvedTypeParameterDefault(typeParameter: TypeParameter): Type | undefined {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [tests/cases/compiler/intersectionApparentTypeCaching.ts] ////
2+
3+
=== intersectionApparentTypeCaching.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/58175
5+
6+
type TX<T extends any[] & object> = T["length"];
7+
>TX : Symbol(TX, Decl(intersectionApparentTypeCaching.ts, 0, 0))
8+
>T : Symbol(T, Decl(intersectionApparentTypeCaching.ts, 2, 8))
9+
>T : Symbol(T, Decl(intersectionApparentTypeCaching.ts, 2, 8))
10+
11+
type T0<U extends any[] & object> = U;
12+
>T0 : Symbol(T0, Decl(intersectionApparentTypeCaching.ts, 2, 48))
13+
>U : Symbol(U, Decl(intersectionApparentTypeCaching.ts, 3, 8))
14+
>U : Symbol(U, Decl(intersectionApparentTypeCaching.ts, 3, 8))
15+
16+
type T1 = T0<string[]>;
17+
>T1 : Symbol(T1, Decl(intersectionApparentTypeCaching.ts, 3, 38))
18+
>T0 : Symbol(T0, Decl(intersectionApparentTypeCaching.ts, 2, 48))
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/intersectionApparentTypeCaching.ts] ////
2+
3+
=== intersectionApparentTypeCaching.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/58175
5+
6+
type TX<T extends any[] & object> = T["length"];
7+
>TX : TX<T>
8+
> : ^^^^^
9+
10+
type T0<U extends any[] & object> = U;
11+
>T0 : U
12+
> : ^
13+
14+
type T1 = T0<string[]>;
15+
>T1 : string[]
16+
> : ^^^^^^^^
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
// https://github.com/microsoft/TypeScript/issues/58175
5+
6+
type TX<T extends any[] & object> = T["length"];
7+
type T0<U extends any[] & object> = U;
8+
type T1 = T0<string[]>;

0 commit comments

Comments
 (0)