Skip to content

Commit

Permalink
Merge pull request #10577 from Microsoft/fixBooleanLiteralTypes
Browse files Browse the repository at this point in the history
Fix boolean literal types
  • Loading branch information
ahejlsberg authored Sep 9, 2016
2 parents 766ecca + d5c0c05 commit 8b1acf6
Show file tree
Hide file tree
Showing 98 changed files with 585 additions and 444 deletions.
6 changes: 4 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2982,7 +2982,9 @@ namespace ts {
if (strictNullChecks && declaration.initializer && !(getFalsyFlags(checkExpressionCached(declaration.initializer)) & TypeFlags.Undefined)) {
type = getTypeWithFacts(type, TypeFacts.NEUndefined);
}
return type;
return declaration.initializer ?
getUnionType([type, checkExpressionCached(declaration.initializer)], /*subtypeReduction*/ true) :
type;
}

function getTypeForVariableLikeDeclarationFromJSDocComment(declaration: VariableLikeDeclaration) {
Expand Down Expand Up @@ -13532,7 +13534,7 @@ namespace ts {
return maybeTypeOfKind(contextualType, (TypeFlags.NumberLiteral | TypeFlags.EnumLiteral));
}
if (type.flags & TypeFlags.Boolean) {
return maybeTypeOfKind(contextualType, TypeFlags.BooleanLiteral) && !isTypeAssignableTo(booleanType, contextualType);
return maybeTypeOfKind(contextualType, TypeFlags.BooleanLiteral);
}
if (type.flags & TypeFlags.Enum) {
return typeContainsLiteralFromEnum(contextualType, <EnumType>type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var y: typeof foo.C1.s1 = false;
>foo : typeof foo
>C1 : typeof foo.C1
>s1 : boolean
>false : boolean
>false : false

var z: foo.M1.I2;
>z : f.I2
Expand Down
28 changes: 14 additions & 14 deletions tests/baselines/reference/arrayBestCommonTypes.types
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,16 @@ module EmptyTypes {
>x : boolean
>y : base
>base : base
>[{ x: true, y: new derived() }, { x: false, y: new base() }] : { x: boolean; y: derived; }[]
>{ x: true, y: new derived() } : { x: boolean; y: derived; }
>x : boolean
>true : boolean
>[{ x: true, y: new derived() }, { x: false, y: new base() }] : ({ x: true; y: derived; } | { x: false; y: base; })[]
>{ x: true, y: new derived() } : { x: true; y: derived; }
>x : true
>true : true
>y : derived
>new derived() : derived
>derived : typeof derived
>{ x: false, y: new base() } : { x: boolean; y: base; }
>x : boolean
>false : boolean
>{ x: false, y: new base() } : { x: false; y: base; }
>x : false
>false : false
>y : base
>new base() : base
>base : typeof base
Expand Down Expand Up @@ -658,16 +658,16 @@ module NonEmptyTypes {
>x : boolean
>y : base
>base : base
>[{ x: true, y: new derived() }, { x: false, y: new base() }] : { x: boolean; y: base; }[]
>{ x: true, y: new derived() } : { x: boolean; y: derived; }
>x : boolean
>true : boolean
>[{ x: true, y: new derived() }, { x: false, y: new base() }] : ({ x: true; y: derived; } | { x: false; y: base; })[]
>{ x: true, y: new derived() } : { x: true; y: derived; }
>x : true
>true : true
>y : derived
>new derived() : derived
>derived : typeof derived
>{ x: false, y: new base() } : { x: boolean; y: base; }
>x : boolean
>false : boolean
>{ x: false, y: new base() } : { x: false; y: base; }
>x : false
>false : false
>y : base
>new base() : base
>base : typeof base
Expand Down
32 changes: 16 additions & 16 deletions tests/baselines/reference/assignmentTypeNarrowing.types
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ x; // string
>x : string

[x] = [true];
>[x] = [true] : [boolean]
>[x] = [true] : [true]
>[x] : [string | number | boolean | RegExp]
>x : string | number | boolean | RegExp
>[true] : [boolean]
>true : boolean
>[true] : [true]
>true : true

x; // boolean
>x : boolean
>x : true

[x = ""] = [1];
>[x = ""] = [1] : [number]
Expand All @@ -34,16 +34,16 @@ x; // string | number
>x : string | number

({x} = {x: true});
>({x} = {x: true}) : { x: boolean; }
>{x} = {x: true} : { x: boolean; }
>({x} = {x: true}) : { x: true; }
>{x} = {x: true} : { x: true; }
>{x} : { x: string | number | boolean | RegExp; }
>x : string | number | boolean | RegExp
>{x: true} : { x: boolean; }
>x : boolean
>true : boolean
>{x: true} : { x: true; }
>x : true
>true : true

x; // boolean
>x : boolean
>x : true

({y: x} = {y: 1});
>({y: x} = {y: 1}) : { y: number; }
Expand All @@ -59,16 +59,16 @@ x; // number
>x : number

({x = ""} = {x: true});
>({x = ""} = {x: true}) : { x?: boolean; }
>{x = ""} = {x: true} : { x?: boolean; }
>({x = ""} = {x: true}) : { x?: true; }
>{x = ""} = {x: true} : { x?: true; }
>{x = ""} : { x?: string | number | boolean | RegExp; }
>x : string | number | boolean | RegExp
>{x: true} : { x?: boolean; }
>x : boolean
>true : boolean
>{x: true} : { x?: true; }
>x : true
>true : true

x; // string | boolean
>x : string | boolean
>x : string | true

({y: x = /a/} = {y: 1});
>({y: x = /a/} = {y: 1}) : { y?: number; }
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/asyncFunctionReturnType.types
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ async function fAsyncExplicit(): Promise<[number, boolean]> {

// This is contextually typed as a tuple.
return [1, true];
>[1, true] : [number, boolean]
>[1, true] : [number, true]
>1 : number
>true : boolean
>true : true
}

2 changes: 1 addition & 1 deletion tests/baselines/reference/bestCommonTypeOfTuple.types
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function f2(x: number): number { return 10; }
function f3(x: number): boolean { return true; }
>f3 : (x: number) => boolean
>x : number
>true : boolean
>true : true

enum E1 { one }
>E1 : E1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var BOOLEAN: boolean;

function foo(): boolean { return true; }
>foo : () => boolean
>true : boolean
>true : true

class A {
>A : A
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/castTest.types
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var a = <any>0;
var b = <boolean>true;
>b : boolean
><boolean>true : boolean
>true : boolean
>true : true

var s = <string>"";
>s : string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class Derived extends Based {

constructor() {
this.y = true;
>this.y = true : boolean
>this.y = true : true
>this.y : boolean
>this : this
>y : boolean
>true : boolean
>true : true
}
}
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,32 +111,32 @@ ANY = undefined, BOOLEAN;
>BOOLEAN : boolean

OBJECT = [1, 2, 3], BOOLEAN = false;
>OBJECT = [1, 2, 3], BOOLEAN = false : boolean
>OBJECT = [1, 2, 3], BOOLEAN = false : false
>OBJECT = [1, 2, 3] : number[]
>OBJECT : Object
>[1, 2, 3] : number[]
>1 : number
>2 : number
>3 : number
>BOOLEAN = false : boolean
>BOOLEAN = false : false
>BOOLEAN : boolean
>false : boolean
>false : false

var resultIsBoolean6 = (null, BOOLEAN);
>resultIsBoolean6 : boolean
>(null, BOOLEAN) : boolean
>null, BOOLEAN : boolean
>resultIsBoolean6 : false
>(null, BOOLEAN) : false
>null, BOOLEAN : false
>null : null
>BOOLEAN : boolean
>BOOLEAN : false

var resultIsBoolean7 = (ANY = undefined, BOOLEAN);
>resultIsBoolean7 : boolean
>(ANY = undefined, BOOLEAN) : boolean
>ANY = undefined, BOOLEAN : boolean
>resultIsBoolean7 : false
>(ANY = undefined, BOOLEAN) : false
>ANY = undefined, BOOLEAN : false
>ANY = undefined : undefined
>ANY : any
>undefined : undefined
>BOOLEAN : boolean
>BOOLEAN : false

var resultIsBoolean8 = (1, true);
>resultIsBoolean8 : boolean
Expand All @@ -154,27 +154,27 @@ var resultIsBoolean9 = (++NUMBER, true);
>true : boolean

var resultIsBoolean10 = ([1, 2, 3], !BOOLEAN);
>resultIsBoolean10 : boolean
>([1, 2, 3], !BOOLEAN) : boolean
>[1, 2, 3], !BOOLEAN : boolean
>resultIsBoolean10 : true
>([1, 2, 3], !BOOLEAN) : true
>[1, 2, 3], !BOOLEAN : true
>[1, 2, 3] : number[]
>1 : number
>2 : number
>3 : number
>!BOOLEAN : boolean
>BOOLEAN : boolean
>!BOOLEAN : true
>BOOLEAN : false

var resultIsBoolean11 = (OBJECT = [1, 2, 3], BOOLEAN = false);
>resultIsBoolean11 : boolean
>(OBJECT = [1, 2, 3], BOOLEAN = false) : boolean
>OBJECT = [1, 2, 3], BOOLEAN = false : boolean
>resultIsBoolean11 : false
>(OBJECT = [1, 2, 3], BOOLEAN = false) : false
>OBJECT = [1, 2, 3], BOOLEAN = false : false
>OBJECT = [1, 2, 3] : number[]
>OBJECT : Object
>[1, 2, 3] : number[]
>1 : number
>2 : number
>3 : number
>BOOLEAN = false : boolean
>BOOLEAN = false : false
>BOOLEAN : boolean
>false : boolean
>false : false

Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ true, 1;

BOOLEAN = false, 1;
>BOOLEAN = false, 1 : number
>BOOLEAN = false : boolean
>BOOLEAN = false : false
>BOOLEAN : boolean
>false : boolean
>false : false
>1 : number

"", NUMBER = 1;
Expand Down Expand Up @@ -146,9 +146,9 @@ var resultIsNumber9 = (BOOLEAN = false, 1);
>resultIsNumber9 : number
>(BOOLEAN = false, 1) : number
>BOOLEAN = false, 1 : number
>BOOLEAN = false : boolean
>BOOLEAN = false : false
>BOOLEAN : boolean
>false : boolean
>false : false
>1 : number

var resultIsNumber10 = ("", NUMBER = 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var y: typeof foo.C1.s1 = false;
>foo : typeof foo
>C1 : typeof foo.C1
>s1 : boolean
>false : boolean
>false : false

var z: foo.M1.I2;
>z : f.I2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/conformance/expressions/assignmentOperator/compoundAdditionAssignmentWithInvalidOperands.ts(6,1): error TS2365: Operator '+=' cannot be applied to types 'boolean' and 'void'.
tests/cases/conformance/expressions/assignmentOperator/compoundAdditionAssignmentWithInvalidOperands.ts(7,1): error TS2365: Operator '+=' cannot be applied to types 'boolean' and 'boolean'.
tests/cases/conformance/expressions/assignmentOperator/compoundAdditionAssignmentWithInvalidOperands.ts(7,1): error TS2365: Operator '+=' cannot be applied to types 'boolean' and 'true'.
tests/cases/conformance/expressions/assignmentOperator/compoundAdditionAssignmentWithInvalidOperands.ts(8,1): error TS2365: Operator '+=' cannot be applied to types 'boolean' and 'number'.
tests/cases/conformance/expressions/assignmentOperator/compoundAdditionAssignmentWithInvalidOperands.ts(9,1): error TS2365: Operator '+=' cannot be applied to types 'boolean' and 'E'.
tests/cases/conformance/expressions/assignmentOperator/compoundAdditionAssignmentWithInvalidOperands.ts(10,1): error TS2365: Operator '+=' cannot be applied to types 'boolean' and '{}'.
Expand Down Expand Up @@ -38,7 +38,7 @@ tests/cases/conformance/expressions/assignmentOperator/compoundAdditionAssignmen
!!! error TS2365: Operator '+=' cannot be applied to types 'boolean' and 'void'.
x1 += true;
~~~~~~~~~~
!!! error TS2365: Operator '+=' cannot be applied to types 'boolean' and 'boolean'.
!!! error TS2365: Operator '+=' cannot be applied to types 'boolean' and 'true'.
x1 += 0;
~~~~~~~
!!! error TS2365: Operator '+=' cannot be applied to types 'boolean' and 'number'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ declare function foo<T>(obj: I<T>): T
foo({
>foo({ p: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]}) : string | number | boolean | (() => void) | number[]
>foo : <T>(obj: I<T>) => T
>{ p: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]} : { [x: string]: string | number | boolean | (() => void) | number[]; [x: number]: number | (() => void) | number[]; 0: () => void; p: string; }
>{ p: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]} : { [x: string]: string | number | true | (() => void) | number[]; [x: number]: number | (() => void) | number[]; 0: () => void; p: string; }

p: "",
>p : string
Expand All @@ -32,7 +32,7 @@ foo({
>"hi" + "bye" : string
>"hi" : string
>"bye" : string
>true : boolean
>true : true

[0 + 1]: 0,
>0 + 1 : number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ declare function foo<T>(obj: I<T>): T
foo({
>foo({ p: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]}) : string | number | boolean | (() => void) | number[]
>foo : <T>(obj: I<T>) => T
>{ p: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]} : { [x: string]: string | number | boolean | (() => void) | number[]; [x: number]: number | (() => void) | number[]; 0: () => void; p: string; }
>{ p: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]} : { [x: string]: string | number | true | (() => void) | number[]; [x: number]: number | (() => void) | number[]; 0: () => void; p: string; }

p: "",
>p : string
Expand All @@ -32,7 +32,7 @@ foo({
>"hi" + "bye" : string
>"hi" : string
>"bye" : string
>true : boolean
>true : true

[0 + 1]: 0,
>0 + 1 : number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ true || false ? exprIsObject1 : exprIsObject2;
>true || false ? exprIsObject1 : exprIsObject2 : Object
>true || false : boolean
>true : boolean
>false : boolean
>false : false
>exprIsObject1 : Object
>exprIsObject2 : Object

Expand Down Expand Up @@ -291,7 +291,7 @@ var resultIsObject3 = true || false ? exprIsObject1 : exprIsObject2;
>true || false ? exprIsObject1 : exprIsObject2 : Object
>true || false : boolean
>true : boolean
>false : boolean
>false : false
>exprIsObject1 : Object
>exprIsObject2 : Object

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/constEnumPropertyAccess1.types
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ var o: {
>idx : number

} = {
>{ 1: true } : { 1: boolean; }
>{ 1: true } : { 1: true; }

1: true
>true : boolean
>true : true

};

Expand Down
Loading

0 comments on commit 8b1acf6

Please sign in to comment.