Skip to content

Commit 9f96fe5

Browse files
committed
Remove the narrow-to-fresh rule added with boolean literals
1 parent 219bb44 commit 9f96fe5

12 files changed

Lines changed: 95 additions & 39 deletions

src/compiler/checker.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14208,10 +14208,7 @@ namespace ts {
1420814208
if (assignedType.flags & TypeFlags.Never) {
1420914209
return assignedType;
1421014210
}
14211-
let reducedType = filterType(declaredType, t => typeMaybeAssignableTo(assignedType, t));
14212-
if (assignedType.flags & (TypeFlags.FreshLiteral | TypeFlags.Literal)) {
14213-
reducedType = mapType(reducedType, getFreshTypeOfLiteralType); // Ensure that if the assignment is a fresh type, that we narrow to fresh types
14214-
}
14211+
const reducedType = filterType(declaredType, t => typeMaybeAssignableTo(assignedType, t));
1421514212
// Our crude heuristic produces an invalid result in some cases: see GH#26130.
1421614213
// For now, when that happens, we give up and don't narrow at all. (This also
1421714214
// means we'll never narrow for erroneous assignments where the assigned type

tests/baselines/reference/commaOperatorWithSecondOperandBooleanType.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ OBJECT = [1, 2, 3], BOOLEAN = false;
122122
>false : false
123123

124124
var resultIsBoolean6 = (null, BOOLEAN);
125-
>resultIsBoolean6 : boolean
125+
>resultIsBoolean6 : false
126126
>(null, BOOLEAN) : false
127127
>null, BOOLEAN : false
128128
>null : null
129129
>BOOLEAN : false
130130

131131
var resultIsBoolean7 = (ANY = undefined, BOOLEAN);
132-
>resultIsBoolean7 : boolean
132+
>resultIsBoolean7 : false
133133
>(ANY = undefined, BOOLEAN) : false
134134
>ANY = undefined, BOOLEAN : false
135135
>ANY = undefined : undefined

tests/baselines/reference/declarationEmitDestructuringObjectLiteralPattern.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ declare var x11: number, y11: string, z11: boolean;
5555
declare function f15(): {
5656
a4: string;
5757
b4: number;
58-
c4: boolean;
58+
c4: true;
5959
};
60-
declare var a4: string, b4: number, c4: boolean;
60+
declare var a4: string, b4: number, c4: true;
6161
declare module m {
62-
var a4: string, b4: number, c4: boolean;
62+
var a4: string, b4: number, c4: true;
6363
}

tests/baselines/reference/declarationEmitDestructuringObjectLiteralPattern.types

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ var { a: x11, b: { a: y11, b: { a: z11 }}} = { a: 1, b: { a: "hello", b: { a: tr
8282
>true : true
8383

8484
function f15() {
85-
>f15 : () => { a4: string; b4: number; c4: boolean; }
85+
>f15 : () => { a4: string; b4: number; c4: true; }
8686

8787
var a4 = "hello";
8888
>a4 : string
@@ -97,25 +97,25 @@ function f15() {
9797
>true : true
9898

9999
return { a4, b4, c4 };
100-
>{ a4, b4, c4 } : { a4: string; b4: number; c4: boolean; }
100+
>{ a4, b4, c4 } : { a4: string; b4: number; c4: true; }
101101
>a4 : string
102102
>b4 : number
103-
>c4 : boolean
103+
>c4 : true
104104
}
105105
var { a4, b4, c4 } = f15();
106106
>a4 : string
107107
>b4 : number
108-
>c4 : boolean
109-
>f15() : { a4: string; b4: number; c4: boolean; }
110-
>f15 : () => { a4: string; b4: number; c4: boolean; }
108+
>c4 : true
109+
>f15() : { a4: string; b4: number; c4: true; }
110+
>f15 : () => { a4: string; b4: number; c4: true; }
111111

112112
module m {
113113
>m : typeof m
114114

115115
export var { a4, b4, c4 } = f15();
116116
>a4 : string
117117
>b4 : number
118-
>c4 : boolean
119-
>f15() : { a4: string; b4: number; c4: boolean; }
120-
>f15 : () => { a4: string; b4: number; c4: boolean; }
118+
>c4 : true
119+
>f15() : { a4: string; b4: number; c4: true; }
120+
>f15 : () => { a4: string; b4: number; c4: true; }
121121
}

tests/baselines/reference/declarationEmitDestructuringObjectLiteralPattern2.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ declare var x11: number, y11: string, z11: boolean;
3434
declare function f15(): {
3535
a4: string;
3636
b4: number;
37-
c4: boolean;
37+
c4: true;
3838
};
39-
declare var a4: string, b4: number, c4: boolean;
39+
declare var a4: string, b4: number, c4: true;
4040
declare module m {
41-
var a4: string, b4: number, c4: boolean;
41+
var a4: string, b4: number, c4: true;
4242
}

tests/baselines/reference/declarationEmitDestructuringObjectLiteralPattern2.types

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var { a: x11, b: { a: y11, b: { a: z11 }}} = { a: 1, b: { a: "hello", b: { a: tr
2121
>true : true
2222

2323
function f15() {
24-
>f15 : () => { a4: string; b4: number; c4: boolean; }
24+
>f15 : () => { a4: string; b4: number; c4: true; }
2525

2626
var a4 = "hello";
2727
>a4 : string
@@ -36,25 +36,25 @@ function f15() {
3636
>true : true
3737

3838
return { a4, b4, c4 };
39-
>{ a4, b4, c4 } : { a4: string; b4: number; c4: boolean; }
39+
>{ a4, b4, c4 } : { a4: string; b4: number; c4: true; }
4040
>a4 : string
4141
>b4 : number
42-
>c4 : boolean
42+
>c4 : true
4343
}
4444
var { a4, b4, c4 } = f15();
4545
>a4 : string
4646
>b4 : number
47-
>c4 : boolean
48-
>f15() : { a4: string; b4: number; c4: boolean; }
49-
>f15 : () => { a4: string; b4: number; c4: boolean; }
47+
>c4 : true
48+
>f15() : { a4: string; b4: number; c4: true; }
49+
>f15 : () => { a4: string; b4: number; c4: true; }
5050

5151
module m {
5252
>m : typeof m
5353

5454
export var { a4, b4, c4 } = f15();
5555
>a4 : string
5656
>b4 : number
57-
>c4 : boolean
58-
>f15() : { a4: string; b4: number; c4: boolean; }
59-
>f15 : () => { a4: string; b4: number; c4: boolean; }
57+
>c4 : true
58+
>f15() : { a4: string; b4: number; c4: true; }
59+
>f15 : () => { a4: string; b4: number; c4: true; }
6060
}

tests/baselines/reference/declarationsAndAssignments.types

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ module M {
483483
}
484484

485485
function f15() {
486-
>f15 : () => { a: string; b: number; c: boolean; }
486+
>f15 : () => { a: string; b: number; c: true; }
487487

488488
var a = "hello";
489489
>a : string
@@ -498,10 +498,10 @@ function f15() {
498498
>true : true
499499

500500
return { a, b, c };
501-
>{ a, b, c } : { a: string; b: number; c: boolean; }
501+
>{ a, b, c } : { a: string; b: number; c: true; }
502502
>a : string
503503
>b : number
504-
>c : boolean
504+
>c : true
505505
}
506506

507507
function f16() {
@@ -510,9 +510,9 @@ function f16() {
510510
var { a, b, c } = f15();
511511
>a : string
512512
>b : number
513-
>c : boolean
514-
>f15() : { a: string; b: number; c: boolean; }
515-
>f15 : () => { a: string; b: number; c: boolean; }
513+
>c : true
514+
>f15() : { a: string; b: number; c: true; }
515+
>f15 : () => { a: string; b: number; c: true; }
516516
}
517517

518518
function f17({ a = "", b = 0, c = false }) {
@@ -547,8 +547,8 @@ f17({ c: true });
547547
f17(f15());
548548
>f17(f15()) : void
549549
>f17 : ({ a, b, c }: { a?: string; b?: number; c?: boolean; }) => void
550-
>f15() : { a: string; b: number; c: boolean; }
551-
>f15 : () => { a: string; b: number; c: boolean; }
550+
>f15() : { a: string; b: number; c: true; }
551+
>f15 : () => { a: string; b: number; c: true; }
552552

553553
function f18() {
554554
>f18 : () => void
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [literalFreshnessNarrowing.ts]
2+
type XY = 'x' | 'y';
3+
const x: XY = 'x';
4+
let x2 = x; // Shouldn't have type string
5+
6+
const y: boolean = true;
7+
let y2 = y; // Shouldn't type boolean
8+
9+
10+
//// [literalFreshnessNarrowing.js]
11+
var x = 'x';
12+
var x2 = x; // Shouldn't have type string
13+
var y = true;
14+
var y2 = y; // Shouldn't type boolean
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/literalFreshnessNarrowing.ts ===
2+
type XY = 'x' | 'y';
3+
>XY : Symbol(XY, Decl(literalFreshnessNarrowing.ts, 0, 0))
4+
5+
const x: XY = 'x';
6+
>x : Symbol(x, Decl(literalFreshnessNarrowing.ts, 1, 5))
7+
>XY : Symbol(XY, Decl(literalFreshnessNarrowing.ts, 0, 0))
8+
9+
let x2 = x; // Shouldn't have type string
10+
>x2 : Symbol(x2, Decl(literalFreshnessNarrowing.ts, 2, 3))
11+
>x : Symbol(x, Decl(literalFreshnessNarrowing.ts, 1, 5))
12+
13+
const y: boolean = true;
14+
>y : Symbol(y, Decl(literalFreshnessNarrowing.ts, 4, 5))
15+
16+
let y2 = y; // Shouldn't type boolean
17+
>y2 : Symbol(y2, Decl(literalFreshnessNarrowing.ts, 5, 3))
18+
>y : Symbol(y, Decl(literalFreshnessNarrowing.ts, 4, 5))
19+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/compiler/literalFreshnessNarrowing.ts ===
2+
type XY = 'x' | 'y';
3+
>XY : XY
4+
5+
const x: XY = 'x';
6+
>x : XY
7+
>'x' : "x"
8+
9+
let x2 = x; // Shouldn't have type string
10+
>x2 : "x"
11+
>x : "x"
12+
13+
const y: boolean = true;
14+
>y : boolean
15+
>true : true
16+
17+
let y2 = y; // Shouldn't type boolean
18+
>y2 : true
19+
>y : true
20+

0 commit comments

Comments
 (0)