Skip to content

Commit a39515f

Browse files
committed
Readd simple preservation fix
1 parent 12ce050 commit a39515f

8 files changed

+32
-178
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7777,7 +7777,7 @@ namespace ts {
77777777
// expression constructs such as array literals and the || and ?: operators). Named types can
77787778
// circularly reference themselves and therefore cannot be subtype reduced during their declaration.
77797779
// For example, "type Item = string | (() => Item" is a named type that circularly references itself.
7780-
function getUnionType(types: Type[], subtypeReduction?: boolean, aliasSymbol?: Symbol, aliasTypeArguments?: Type[]): Type {
7780+
function getUnionType(types: Type[], subtypeReduction?: boolean, aliasSymbol?: Symbol, aliasTypeArguments?: Type[], noReductions?: boolean): Type {
77817781
if (types.length === 0) {
77827782
return neverType;
77837783
}
@@ -7789,11 +7789,13 @@ namespace ts {
77897789
if (typeSet.containsAny) {
77907790
return anyType;
77917791
}
7792-
if (subtypeReduction) {
7793-
removeSubtypes(typeSet);
7794-
}
7795-
else if (typeSet.containsLiteralOrUniqueESSymbol) {
7796-
removeRedundantLiteralTypes(typeSet);
7792+
if (!noReductions) {
7793+
if (subtypeReduction) {
7794+
removeSubtypes(typeSet);
7795+
}
7796+
else if (typeSet.containsLiteralOrUniqueESSymbol) {
7797+
removeRedundantLiteralTypes(typeSet);
7798+
}
77977799
}
77987800
if (typeSet.length === 0) {
77997801
return typeSet.containsNull ? typeSet.containsNonWideningType ? nullType : nullWideningType :
@@ -12104,7 +12106,7 @@ namespace ts {
1210412106
// Apply a mapping function to a type and return the resulting type. If the source type
1210512107
// is a union type, the mapping function is applied to each constituent type and a union
1210612108
// of the resulting types is returned.
12107-
function mapType(type: Type, mapper: (t: Type) => Type): Type {
12109+
function mapType(type: Type, mapper: (t: Type) => Type, noReductions?: boolean): Type {
1210812110
if (!(type.flags & TypeFlags.Union)) {
1210912111
return mapper(type);
1211012112
}
@@ -12125,7 +12127,7 @@ namespace ts {
1212512127
}
1212612128
}
1212712129
}
12128-
return mappedTypes ? getUnionType(mappedTypes) : mappedType;
12130+
return mappedTypes ? getUnionType(mappedTypes, /*subtypeReduction*/ undefined, /*aliasSymbol*/ undefined, /*aliasTypeArguments*/ undefined, noReductions) : mappedType;
1212912131
}
1213012132

1213112133
function extractTypesOfKind(type: Type, kind: TypeFlags) {
@@ -13911,11 +13913,11 @@ namespace ts {
1391113913
return mapType(type, t => {
1391213914
const prop = t.flags & TypeFlags.StructuredType ? getPropertyOfType(t, name) : undefined;
1391313915
return prop ? getTypeOfSymbol(prop) : undefined;
13914-
});
13916+
}, /*noReductions*/ true);
1391513917
}
1391613918

1391713919
function getIndexTypeOfContextualType(type: Type, kind: IndexKind) {
13918-
return mapType(type, t => getIndexTypeOfStructuredType(t, kind));
13920+
return mapType(type, t => getIndexTypeOfStructuredType(t, kind), /*noReductions*/ true);
1391913921
}
1392013922

1392113923
// Return true if the given contextual type is a tuple-like type

tests/baselines/reference/contextualTypeShouldBeLiteral.errors.txt

Lines changed: 0 additions & 114 deletions
This file was deleted.

tests/baselines/reference/contextualTypeShouldBeLiteral.symbols

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ foo2({
104104

105105
this;
106106
this.value;
107+
>this.value : Symbol(value, Decl(contextualTypeShouldBeLiteral.ts, 25, 15), Decl(contextualTypeShouldBeLiteral.ts, 31, 15))
108+
>value : Symbol(value, Decl(contextualTypeShouldBeLiteral.ts, 25, 15), Decl(contextualTypeShouldBeLiteral.ts, 31, 15))
107109
}
108110
});
109111

tests/baselines/reference/contextualTypeShouldBeLiteral.types

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function foo2(bar: X2 | Y2) { }
9797
foo2({
9898
>foo2({ type2: 'y', value: 'done', method() { this; this.value; }}) : void
9999
>foo2 : (bar: X2 | Y2) => void
100-
>{ type2: 'y', value: 'done', method() { this; this.value; }} : { type2: string; value: string; method(): void; }
100+
>{ type2: 'y', value: 'done', method() { this; this.value; }} : { type2: "y"; value: "done"; method(): void; }
101101

102102
type2: 'y',
103103
>type2 : string
@@ -111,12 +111,12 @@ foo2({
111111
>method : () => void
112112

113113
this;
114-
>this : any
114+
>this : X2 | Y2
115115

116116
this.value;
117-
>this.value : any
118-
>this : any
119-
>value : any
117+
>this.value : string
118+
>this : X2 | Y2
119+
>value : string
120120
}
121121
});
122122

tests/baselines/reference/nestedTypeVariableInfersLiteral.errors.txt

Lines changed: 0 additions & 37 deletions
This file was deleted.

tests/baselines/reference/nestedTypeVariableInfersLiteral.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ hasZField(directUnionSingle) // ok
1616
hasZField(directUnionArray) // ok
1717
hasZField(nestedSingle) // ok
1818
hasZField(nestedUnionSingle) // ok
19-
hasZField(nestedUnionArray) // ok
19+
hasZField(nestedUnionArray) // ok
20+
2021

2122
//// [nestedTypeVariableInfersLiteral.js]
2223
var directUnionSingle = direct("z");

tests/baselines/reference/nestedTypeVariableInfersLiteral.types

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ const directUnionSingle = direct("z")
3535
>"z" : "z"
3636

3737
const directUnionArray = direct(["z", "y"])
38-
>directUnionArray : any
39-
>direct(["z", "y"]) : any
38+
>directUnionArray : Record<"z" | "y", string>
39+
>direct(["z", "y"]) : Record<"z" | "y", string>
4040
>direct : <A extends string>(a: A | A[]) => Record<A, string>
41-
>["z", "y"] : string[]
41+
>["z", "y"] : ("z" | "y")[]
4242
>"z" : "z"
4343
>"y" : "y"
4444

@@ -59,12 +59,12 @@ const nestedUnionSingle = nestedUnion({fields: "z"})
5959
>"z" : "z"
6060

6161
const nestedUnionArray = nestedUnion({fields: ["z", "y"]})
62-
>nestedUnionArray : any
63-
>nestedUnion({fields: ["z", "y"]}) : any
62+
>nestedUnionArray : Record<"z" | "y", string>
63+
>nestedUnion({fields: ["z", "y"]}) : Record<"z" | "y", string>
6464
>nestedUnion : <A extends string>(a: { fields: A | A[]; }) => Record<A, string>
65-
>{fields: ["z", "y"]} : { fields: string[]; }
66-
>fields : string[]
67-
>["z", "y"] : string[]
65+
>{fields: ["z", "y"]} : { fields: ("z" | "y")[]; }
66+
>fields : ("z" | "y")[]
67+
>["z", "y"] : ("z" | "y")[]
6868
>"z" : "z"
6969
>"y" : "y"
7070

@@ -81,7 +81,7 @@ hasZField(directUnionSingle) // ok
8181
hasZField(directUnionArray) // ok
8282
>hasZField(directUnionArray) : void
8383
>hasZField : (arg: { z: string; }) => void
84-
>directUnionArray : any
84+
>directUnionArray : Record<"z" | "y", string>
8585

8686
hasZField(nestedSingle) // ok
8787
>hasZField(nestedSingle) : void
@@ -96,5 +96,5 @@ hasZField(nestedUnionSingle) // ok
9696
hasZField(nestedUnionArray) // ok
9797
>hasZField(nestedUnionArray) : void
9898
>hasZField : (arg: { z: string; }) => void
99-
>nestedUnionArray : any
99+
>nestedUnionArray : Record<"z" | "y", string>
100100

tests/cases/compiler/nestedTypeVariableInfersLiteral.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ hasZField(directUnionSingle) // ok
1515
hasZField(directUnionArray) // ok
1616
hasZField(nestedSingle) // ok
1717
hasZField(nestedUnionSingle) // ok
18-
hasZField(nestedUnionArray) // ok
18+
hasZField(nestedUnionArray) // ok

0 commit comments

Comments
 (0)