Remove the narrow-to-fresh rule added with boolean literals

This commit is contained in:
Wesley Wigham 2018-09-21 11:04:52 -07:00
parent 219bb44b4d
commit 9f96fe5da3
No known key found for this signature in database
GPG key ID: D59F87F60C5400C9
12 changed files with 95 additions and 39 deletions

View file

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

View file

@ -122,14 +122,14 @@ OBJECT = [1, 2, 3], BOOLEAN = false;
>false : false
var resultIsBoolean6 = (null, BOOLEAN);
>resultIsBoolean6 : boolean
>resultIsBoolean6 : false
>(null, BOOLEAN) : false
>null, BOOLEAN : false
>null : null
>BOOLEAN : false
var resultIsBoolean7 = (ANY = undefined, BOOLEAN);
>resultIsBoolean7 : boolean
>resultIsBoolean7 : false
>(ANY = undefined, BOOLEAN) : false
>ANY = undefined, BOOLEAN : false
>ANY = undefined : undefined

View file

@ -55,9 +55,9 @@ declare var x11: number, y11: string, z11: boolean;
declare function f15(): {
a4: string;
b4: number;
c4: boolean;
c4: true;
};
declare var a4: string, b4: number, c4: boolean;
declare var a4: string, b4: number, c4: true;
declare module m {
var a4: string, b4: number, c4: boolean;
var a4: string, b4: number, c4: true;
}

View file

@ -82,7 +82,7 @@ var { a: x11, b: { a: y11, b: { a: z11 }}} = { a: 1, b: { a: "hello", b: { a: tr
>true : true
function f15() {
>f15 : () => { a4: string; b4: number; c4: boolean; }
>f15 : () => { a4: string; b4: number; c4: true; }
var a4 = "hello";
>a4 : string
@ -97,17 +97,17 @@ function f15() {
>true : true
return { a4, b4, c4 };
>{ a4, b4, c4 } : { a4: string; b4: number; c4: boolean; }
>{ a4, b4, c4 } : { a4: string; b4: number; c4: true; }
>a4 : string
>b4 : number
>c4 : boolean
>c4 : true
}
var { a4, b4, c4 } = f15();
>a4 : string
>b4 : number
>c4 : boolean
>f15() : { a4: string; b4: number; c4: boolean; }
>f15 : () => { a4: string; b4: number; c4: boolean; }
>c4 : true
>f15() : { a4: string; b4: number; c4: true; }
>f15 : () => { a4: string; b4: number; c4: true; }
module m {
>m : typeof m
@ -115,7 +115,7 @@ module m {
export var { a4, b4, c4 } = f15();
>a4 : string
>b4 : number
>c4 : boolean
>f15() : { a4: string; b4: number; c4: boolean; }
>f15 : () => { a4: string; b4: number; c4: boolean; }
>c4 : true
>f15() : { a4: string; b4: number; c4: true; }
>f15 : () => { a4: string; b4: number; c4: true; }
}

View file

@ -34,9 +34,9 @@ declare var x11: number, y11: string, z11: boolean;
declare function f15(): {
a4: string;
b4: number;
c4: boolean;
c4: true;
};
declare var a4: string, b4: number, c4: boolean;
declare var a4: string, b4: number, c4: true;
declare module m {
var a4: string, b4: number, c4: boolean;
var a4: string, b4: number, c4: true;
}

View file

@ -21,7 +21,7 @@ var { a: x11, b: { a: y11, b: { a: z11 }}} = { a: 1, b: { a: "hello", b: { a: tr
>true : true
function f15() {
>f15 : () => { a4: string; b4: number; c4: boolean; }
>f15 : () => { a4: string; b4: number; c4: true; }
var a4 = "hello";
>a4 : string
@ -36,17 +36,17 @@ function f15() {
>true : true
return { a4, b4, c4 };
>{ a4, b4, c4 } : { a4: string; b4: number; c4: boolean; }
>{ a4, b4, c4 } : { a4: string; b4: number; c4: true; }
>a4 : string
>b4 : number
>c4 : boolean
>c4 : true
}
var { a4, b4, c4 } = f15();
>a4 : string
>b4 : number
>c4 : boolean
>f15() : { a4: string; b4: number; c4: boolean; }
>f15 : () => { a4: string; b4: number; c4: boolean; }
>c4 : true
>f15() : { a4: string; b4: number; c4: true; }
>f15 : () => { a4: string; b4: number; c4: true; }
module m {
>m : typeof m
@ -54,7 +54,7 @@ module m {
export var { a4, b4, c4 } = f15();
>a4 : string
>b4 : number
>c4 : boolean
>f15() : { a4: string; b4: number; c4: boolean; }
>f15 : () => { a4: string; b4: number; c4: boolean; }
>c4 : true
>f15() : { a4: string; b4: number; c4: true; }
>f15 : () => { a4: string; b4: number; c4: true; }
}

View file

@ -483,7 +483,7 @@ module M {
}
function f15() {
>f15 : () => { a: string; b: number; c: boolean; }
>f15 : () => { a: string; b: number; c: true; }
var a = "hello";
>a : string
@ -498,10 +498,10 @@ function f15() {
>true : true
return { a, b, c };
>{ a, b, c } : { a: string; b: number; c: boolean; }
>{ a, b, c } : { a: string; b: number; c: true; }
>a : string
>b : number
>c : boolean
>c : true
}
function f16() {
@ -510,9 +510,9 @@ function f16() {
var { a, b, c } = f15();
>a : string
>b : number
>c : boolean
>f15() : { a: string; b: number; c: boolean; }
>f15 : () => { a: string; b: number; c: boolean; }
>c : true
>f15() : { a: string; b: number; c: true; }
>f15 : () => { a: string; b: number; c: true; }
}
function f17({ a = "", b = 0, c = false }) {
@ -547,8 +547,8 @@ f17({ c: true });
f17(f15());
>f17(f15()) : void
>f17 : ({ a, b, c }: { a?: string; b?: number; c?: boolean; }) => void
>f15() : { a: string; b: number; c: boolean; }
>f15 : () => { a: string; b: number; c: boolean; }
>f15() : { a: string; b: number; c: true; }
>f15 : () => { a: string; b: number; c: true; }
function f18() {
>f18 : () => void

View file

@ -0,0 +1,14 @@
//// [literalFreshnessNarrowing.ts]
type XY = 'x' | 'y';
const x: XY = 'x';
let x2 = x; // Shouldn't have type string
const y: boolean = true;
let y2 = y; // Shouldn't type boolean
//// [literalFreshnessNarrowing.js]
var x = 'x';
var x2 = x; // Shouldn't have type string
var y = true;
var y2 = y; // Shouldn't type boolean

View file

@ -0,0 +1,19 @@
=== tests/cases/compiler/literalFreshnessNarrowing.ts ===
type XY = 'x' | 'y';
>XY : Symbol(XY, Decl(literalFreshnessNarrowing.ts, 0, 0))
const x: XY = 'x';
>x : Symbol(x, Decl(literalFreshnessNarrowing.ts, 1, 5))
>XY : Symbol(XY, Decl(literalFreshnessNarrowing.ts, 0, 0))
let x2 = x; // Shouldn't have type string
>x2 : Symbol(x2, Decl(literalFreshnessNarrowing.ts, 2, 3))
>x : Symbol(x, Decl(literalFreshnessNarrowing.ts, 1, 5))
const y: boolean = true;
>y : Symbol(y, Decl(literalFreshnessNarrowing.ts, 4, 5))
let y2 = y; // Shouldn't type boolean
>y2 : Symbol(y2, Decl(literalFreshnessNarrowing.ts, 5, 3))
>y : Symbol(y, Decl(literalFreshnessNarrowing.ts, 4, 5))

View file

@ -0,0 +1,20 @@
=== tests/cases/compiler/literalFreshnessNarrowing.ts ===
type XY = 'x' | 'y';
>XY : XY
const x: XY = 'x';
>x : XY
>'x' : "x"
let x2 = x; // Shouldn't have type string
>x2 : "x"
>x : "x"
const y: boolean = true;
>y : boolean
>true : true
let y2 = y; // Shouldn't type boolean
>y2 : true
>y : true

View file

@ -92,7 +92,7 @@ namespace Test {
>true : true
let x = file.isFile;
>x : boolean
>x : true
>file.isFile : true
>file : FileSystemObject
>isFile : true

View file

@ -0,0 +1,6 @@
type XY = 'x' | 'y';
const x: XY = 'x';
let x2 = x; // Shouldn't have type string
const y: boolean = true;
let y2 = y; // Shouldn't type boolean