Add new method on interface Boolean to ensure it is not assignable from other types

This commit is contained in:
Mohamed Hegazy 2014-12-08 11:22:52 -08:00
parent 44eca093a6
commit 80c5acc04e
7 changed files with 88 additions and 3 deletions

2
src/lib/core.d.ts vendored
View file

@ -426,6 +426,8 @@ interface StringConstructor {
declare var String: StringConstructor;
interface Boolean {
/** Returns the primitive value of the specified object. */
valueOf(): boolean;
}
interface BooleanConstructor {

View file

@ -1,8 +1,12 @@
tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface2.ts(14,1): error TS2322: Type 'NotBoolean' is not assignable to type 'Boolean'.
Types of property 'valueOf' are incompatible.
Type '() => Object' is not assignable to type '() => boolean'.
Type 'Object' is not assignable to type 'boolean'.
tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface2.ts(19,1): error TS2322: Type 'Boolean' is not assignable to type 'boolean'.
tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface2.ts(20,1): error TS2322: Type 'NotBoolean' is not assignable to type 'boolean'.
==== tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface2.ts (2 errors) ====
==== tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface2.ts (3 errors) ====
interface Boolean {
doStuff(): string;
}
@ -17,6 +21,11 @@ tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface2.ts(
a = x;
a = b;
~
!!! error TS2322: Type 'NotBoolean' is not assignable to type 'Boolean'.
!!! error TS2322: Types of property 'valueOf' are incompatible.
!!! error TS2322: Type '() => Object' is not assignable to type '() => boolean'.
!!! error TS2322: Type 'Object' is not assignable to type 'boolean'.
b = a;
b = x;

View file

@ -0,0 +1,38 @@
tests/cases/compiler/booleanAssignment.ts(2,1): error TS2322: Type 'number' is not assignable to type 'Boolean'.
Types of property 'valueOf' are incompatible.
Type '() => Object' is not assignable to type '() => boolean'.
Type 'Object' is not assignable to type 'boolean'.
tests/cases/compiler/booleanAssignment.ts(3,1): error TS2322: Type 'string' is not assignable to type 'Boolean'.
Types of property 'valueOf' are incompatible.
Type '() => Object' is not assignable to type '() => boolean'.
tests/cases/compiler/booleanAssignment.ts(4,1): error TS2322: Type '{}' is not assignable to type 'Boolean'.
Types of property 'valueOf' are incompatible.
Type '() => Object' is not assignable to type '() => boolean'.
==== tests/cases/compiler/booleanAssignment.ts (3 errors) ====
var b = new Boolean();
b = 1; // Error
~
!!! error TS2322: Type 'number' is not assignable to type 'Boolean'.
!!! error TS2322: Types of property 'valueOf' are incompatible.
!!! error TS2322: Type '() => Object' is not assignable to type '() => boolean'.
!!! error TS2322: Type 'Object' is not assignable to type 'boolean'.
b = "a"; // Error
~
!!! error TS2322: Type 'string' is not assignable to type 'Boolean'.
!!! error TS2322: Types of property 'valueOf' are incompatible.
!!! error TS2322: Type '() => Object' is not assignable to type '() => boolean'.
b = {}; // Error
~
!!! error TS2322: Type '{}' is not assignable to type 'Boolean'.
!!! error TS2322: Types of property 'valueOf' are incompatible.
!!! error TS2322: Type '() => Object' is not assignable to type '() => boolean'.
var o = {};
o = b; // OK
b = true; // OK
var b2:boolean;
b = b2; // OK

View file

@ -0,0 +1,24 @@
//// [booleanAssignment.ts]
var b = new Boolean();
b = 1; // Error
b = "a"; // Error
b = {}; // Error
var o = {};
o = b; // OK
b = true; // OK
var b2:boolean;
b = b2; // OK
//// [booleanAssignment.js]
var b = new Boolean();
b = 1; // Error
b = "a"; // Error
b = {}; // Error
var o = {};
o = b; // OK
b = true; // OK
var b2;
b = b2; // OK

View file

@ -1,5 +1,5 @@
tests/cases/compiler/templateStringsArrayTypeDefinedInES5Mode.ts(10,3): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
lib.d.ts(513,11): error TS2300: Duplicate identifier 'TemplateStringsArray'.
lib.d.ts(515,11): error TS2300: Duplicate identifier 'TemplateStringsArray'.
tests/cases/compiler/templateStringsArrayTypeDefinedInES5Mode.ts(2,7): error TS2300: Duplicate identifier 'TemplateStringsArray'.
tests/cases/compiler/templateStringsArrayTypeDefinedInES5Mode.ts(8,3): error TS2345: Argument of type '{ [x: number]: undefined; }' is not assignable to parameter of type 'TemplateStringsArray'.
Property 'raw' is missing in type '{ [x: number]: undefined; }'.

View file

@ -1,4 +1,4 @@
lib.d.ts(513,11): error TS2300: Duplicate identifier 'TemplateStringsArray'.
lib.d.ts(515,11): error TS2300: Duplicate identifier 'TemplateStringsArray'.
tests/cases/compiler/templateStringsArrayTypeRedefinedInES6Mode.ts(2,7): error TS2300: Duplicate identifier 'TemplateStringsArray'.
tests/cases/compiler/templateStringsArrayTypeRedefinedInES6Mode.ts(8,3): error TS2345: Argument of type '{ [x: number]: undefined; }' is not assignable to parameter of type 'TemplateStringsArray'.
Property 'raw' is missing in type '{ [x: number]: undefined; }'.

View file

@ -0,0 +1,12 @@
var b = new Boolean();
b = 1; // Error
b = "a"; // Error
b = {}; // Error
var o = {};
o = b; // OK
b = true; // OK
var b2:boolean;
b = b2; // OK