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
This commit is contained in:
Anders Hejlsberg 2021-04-17 04:07:46 -10:00 committed by GitHub
parent 01264ac414
commit 0f2dabcd4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 8 deletions

View File

@ -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:

View File

@ -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>'.
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>'.
@ -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.
}

View File

@ -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
@ -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