Skip to content

Commit 0f2dabc

Browse files
authored
Don't re-alias top-level type aliases with local type aliases (#43701)
* No re-aliasing of top-level type aliases with local type aliases * Accept new baselines
1 parent 01264ac commit 0f2dabc

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12834,12 +12834,22 @@ namespace ts {
1283412834
typeParameters.length);
1283512835
return errorType;
1283612836
}
12837+
// We refrain from associating a local type alias with an instantiation of a top-level type alias
12838+
// because the local alias may end up being referenced in an inferred return type where it is not
12839+
// accessible--which in turn may lead to a large structural expansion of the type when generating
12840+
// a .d.ts file. See #43622 for an example.
1283712841
const aliasSymbol = getAliasSymbolForTypeNode(node);
12838-
return getTypeAliasInstantiation(symbol, typeArgumentsFromTypeReferenceNode(node), aliasSymbol, getTypeArgumentsForAliasSymbol(aliasSymbol));
12842+
const newAliasSymbol = aliasSymbol && (isLocalTypeAlias(symbol) || !isLocalTypeAlias(aliasSymbol)) ? aliasSymbol : undefined;
12843+
return getTypeAliasInstantiation(symbol, typeArgumentsFromTypeReferenceNode(node), newAliasSymbol, getTypeArgumentsForAliasSymbol(newAliasSymbol));
1283912844
}
1284012845
return checkNoTypeArguments(node, symbol) ? type : errorType;
1284112846
}
1284212847

12848+
function isLocalTypeAlias(symbol: Symbol) {
12849+
const declaration = symbol.declarations?.find(isTypeAlias);
12850+
return !!(declaration && getContainingFunction(declaration));
12851+
}
12852+
1284312853
function getTypeReferenceName(node: TypeReferenceType): EntityNameOrEntityNameExpression | undefined {
1284412854
switch (node.kind) {
1284512855
case SyntaxKind.TypeReference:

tests/baselines/reference/conditionalTypes1.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(159,5): error TS2
5050
tests/cases/conformance/types/conditional/conditionalTypes1.ts(160,5): error TS2322: Type 'T' is not assignable to type 'ZeroOf<T>'.
5151
Type 'string | number' is not assignable to type 'ZeroOf<T>'.
5252
Type 'string' is not assignable to type 'ZeroOf<T>'.
53-
tests/cases/conformance/types/conditional/conditionalTypes1.ts(263,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'z' must be of type 'T1', but here has type 'T2'.
53+
tests/cases/conformance/types/conditional/conditionalTypes1.ts(263,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'z' must be of type 'T1', but here has type 'Foo<T & U>'.
5454
tests/cases/conformance/types/conditional/conditionalTypes1.ts(288,43): error TS2322: Type 'T95<U>' is not assignable to type 'T94<U>'.
5555
Type 'number | boolean' is not assignable to type 'T94<U>'.
5656
Type 'number' is not assignable to type 'T94<U>'.
@@ -392,7 +392,7 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(288,43): error TS
392392
var z: T1;
393393
var z: T2; // Error, T2 is distributive, T1 isn't
394394
~
395-
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'z' must be of type 'T1', but here has type 'T2'.
395+
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'z' must be of type 'T1', but here has type 'Foo<T & U>'.
396396
!!! related TS6203 tests/cases/conformance/types/conditional/conditionalTypes1.ts:262:9: 'z' was also declared here.
397397
}
398398

tests/baselines/reference/conditionalTypes1.types

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ function f32<T, U>() {
816816
>T1 : T & U extends string ? boolean : number
817817

818818
type T2 = Foo<T & U>;
819-
>T2 : T & U extends string ? boolean : number
819+
>T2 : Foo<T & U>
820820

821821
var z: T1;
822822
>z : T & U extends string ? boolean : number
@@ -829,16 +829,16 @@ function f33<T, U>() {
829829
>f33 : <T, U>() => void
830830

831831
type T1 = Foo<T & U>;
832-
>T1 : T & U extends string ? boolean : number
832+
>T1 : Foo<T & U>
833833

834834
type T2 = Bar<T & U>;
835-
>T2 : T & U extends string ? boolean : number
835+
>T2 : Bar<T & U>
836836

837837
var z: T1;
838-
>z : T & U extends string ? boolean : number
838+
>z : Foo<T & U>
839839

840840
var z: T2;
841-
>z : T & U extends string ? boolean : number
841+
>z : Foo<T & U>
842842
}
843843

844844
// Repro from #21823

0 commit comments

Comments
 (0)