From 80c5acc04ef75911547376276d13d5bf15663cc4 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Mon, 8 Dec 2014 11:22:52 -0800 Subject: [PATCH] Add new method on interface Boolean to ensure it is not assignable from other types --- src/lib/core.d.ts | 2 + .../assignFromBooleanInterface2.errors.txt | 11 +++++- .../reference/booleanAssignment.errors.txt | 38 +++++++++++++++++++ .../baselines/reference/booleanAssignment.js | 24 ++++++++++++ ...tringsArrayTypeDefinedInES5Mode.errors.txt | 2 +- ...ingsArrayTypeRedefinedInES6Mode.errors.txt | 2 +- tests/cases/compiler/booleanAssignment.ts | 12 ++++++ 7 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 tests/baselines/reference/booleanAssignment.errors.txt create mode 100644 tests/baselines/reference/booleanAssignment.js create mode 100644 tests/cases/compiler/booleanAssignment.ts diff --git a/src/lib/core.d.ts b/src/lib/core.d.ts index 1c87984bc2757..cb5324fbd3da1 100644 --- a/src/lib/core.d.ts +++ b/src/lib/core.d.ts @@ -426,6 +426,8 @@ interface StringConstructor { declare var String: StringConstructor; interface Boolean { + /** Returns the primitive value of the specified object. */ + valueOf(): boolean; } interface BooleanConstructor { diff --git a/tests/baselines/reference/assignFromBooleanInterface2.errors.txt b/tests/baselines/reference/assignFromBooleanInterface2.errors.txt index 66d55e00d8641..c03eab60c7477 100644 --- a/tests/baselines/reference/assignFromBooleanInterface2.errors.txt +++ b/tests/baselines/reference/assignFromBooleanInterface2.errors.txt @@ -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; diff --git a/tests/baselines/reference/booleanAssignment.errors.txt b/tests/baselines/reference/booleanAssignment.errors.txt new file mode 100644 index 0000000000000..2d6c6b045ea6e --- /dev/null +++ b/tests/baselines/reference/booleanAssignment.errors.txt @@ -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 \ No newline at end of file diff --git a/tests/baselines/reference/booleanAssignment.js b/tests/baselines/reference/booleanAssignment.js new file mode 100644 index 0000000000000..573367a429357 --- /dev/null +++ b/tests/baselines/reference/booleanAssignment.js @@ -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 diff --git a/tests/baselines/reference/templateStringsArrayTypeDefinedInES5Mode.errors.txt b/tests/baselines/reference/templateStringsArrayTypeDefinedInES5Mode.errors.txt index b15a867a79e2a..dc494c7c3f44e 100644 --- a/tests/baselines/reference/templateStringsArrayTypeDefinedInES5Mode.errors.txt +++ b/tests/baselines/reference/templateStringsArrayTypeDefinedInES5Mode.errors.txt @@ -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; }'. diff --git a/tests/baselines/reference/templateStringsArrayTypeRedefinedInES6Mode.errors.txt b/tests/baselines/reference/templateStringsArrayTypeRedefinedInES6Mode.errors.txt index 248ea95e3480b..db383b216a55f 100644 --- a/tests/baselines/reference/templateStringsArrayTypeRedefinedInES6Mode.errors.txt +++ b/tests/baselines/reference/templateStringsArrayTypeRedefinedInES6Mode.errors.txt @@ -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; }'. diff --git a/tests/cases/compiler/booleanAssignment.ts b/tests/cases/compiler/booleanAssignment.ts new file mode 100644 index 0000000000000..619b16bc419fd --- /dev/null +++ b/tests/cases/compiler/booleanAssignment.ts @@ -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 \ No newline at end of file