Skip to content

Commit 108a851

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

File tree

4 files changed

+18
-128
lines changed

4 files changed

+18
-128
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 9 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,7 +13913,7 @@ 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) {

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

0 commit comments

Comments
 (0)