Skip to content
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

Don't re-alias top-level type aliases with local type aliases #43701

Merged
merged 2 commits into from
Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12834,12 +12834,22 @@ namespace ts {
typeParameters.length);
return errorType;
}
// We refrain from associating a local type alias with an instantiation of a top-level type alias
// because the local alias may end up being referenced in an inferred return type where it is not
// accessible--which in turn may lead to a large structural expansion of the type when generating
// a .d.ts file. See #43622 for an example.
const aliasSymbol = getAliasSymbolForTypeNode(node);
return getTypeAliasInstantiation(symbol, typeArgumentsFromTypeReferenceNode(node), aliasSymbol, getTypeArgumentsForAliasSymbol(aliasSymbol));
const newAliasSymbol = aliasSymbol && (isLocalTypeAlias(symbol) || !isLocalTypeAlias(aliasSymbol)) ? aliasSymbol : undefined;
return getTypeAliasInstantiation(symbol, typeArgumentsFromTypeReferenceNode(node), newAliasSymbol, getTypeArgumentsForAliasSymbol(newAliasSymbol));
}
return checkNoTypeArguments(node, symbol) ? type : errorType;
}

function isLocalTypeAlias(symbol: Symbol) {
const declaration = symbol.declarations?.find(isTypeAlias);
return !!(declaration && getContainingFunction(declaration));
}

function getTypeReferenceName(node: TypeReferenceType): EntityNameOrEntityNameExpression | undefined {
switch (node.kind) {
case SyntaxKind.TypeReference:
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/conditionalTypes1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(159,5): error TS2
tests/cases/conformance/types/conditional/conditionalTypes1.ts(160,5): error TS2322: Type 'T' is not assignable to type 'ZeroOf<T>'.
Type 'string | number' is not assignable to type 'ZeroOf<T>'.
Type 'string' is not assignable to type 'ZeroOf<T>'.
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'.
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>'.
Copy link
Member

Choose a reason for hiding this comment

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

This seems worse, but I believe I understood you to say it's just a return to our old behavior?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it's a return to the old behavior, the rationale being that Foo<xxx> is accessible outside the function whereas T2 is not. I think very little code will be affected by this change, as it is relatively rare to have local type aliases.

tests/cases/conformance/types/conditional/conditionalTypes1.ts(288,43): error TS2322: Type 'T95<U>' is not assignable to type 'T94<U>'.
Type 'number | boolean' is not assignable to type 'T94<U>'.
Type 'number' is not assignable to type 'T94<U>'.
Expand Down Expand Up @@ -392,7 +392,7 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(288,43): error TS
var z: T1;
var z: T2; // Error, T2 is distributive, T1 isn't
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'z' must be of type 'T1', but here has type 'T2'.
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'z' must be of type 'T1', but here has type 'Foo<T & U>'.
!!! related TS6203 tests/cases/conformance/types/conditional/conditionalTypes1.ts:262:9: 'z' was also declared here.
}

Expand Down
10 changes: 5 additions & 5 deletions tests/baselines/reference/conditionalTypes1.types
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ function f32<T, U>() {
>T1 : T & U extends string ? boolean : number

type T2 = Foo<T & U>;
>T2 : T & U extends string ? boolean : number
>T2 : Foo<T & U>

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

type T1 = Foo<T & U>;
>T1 : T & U extends string ? boolean : number
>T1 : Foo<T & U>

type T2 = Bar<T & U>;
>T2 : T & U extends string ? boolean : number
>T2 : Bar<T & U>

var z: T1;
>z : T & U extends string ? boolean : number
>z : Foo<T & U>

var z: T2;
>z : T & U extends string ? boolean : number
>z : Foo<T & U>
}

// Repro from #21823
Expand Down