From d3c572c1873e767ef6ce677a0b0b956f134e0802 Mon Sep 17 00:00:00 2001 From: Forbes Lindesay Date: Mon, 18 Feb 2019 13:20:19 +0000 Subject: [PATCH 1/2] Allow Boolean() to be used to perform a null check --- src/lib/es5.d.ts | 2 +- .../expressions/typeGuards/typeGuardBoolean.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardBoolean.ts diff --git a/src/lib/es5.d.ts b/src/lib/es5.d.ts index 4112f5a2ae7d7..18a98d1507d26 100644 --- a/src/lib/es5.d.ts +++ b/src/lib/es5.d.ts @@ -513,7 +513,7 @@ interface Boolean { interface BooleanConstructor { new(value?: any): Boolean; - (value?: any): boolean; + (value?: T): value is Exclude; readonly prototype: Boolean; } diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardBoolean.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardBoolean.ts new file mode 100644 index 0000000000000..3f2dde227ba8c --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardBoolean.ts @@ -0,0 +1,14 @@ +// @strictNullChecks: true +function test(strOrNull: string | null, strOrUndefined: string | undefined) { + var str: string = "original"; + var nil: null; + if (!Boolean(strOrNull)) { + nil = strOrNull; + } + else { + str = strOrNull; + } + if (Boolean(strOrUndefined)) { + str = strOrUndefined; + } +} From 24e7e1c76023bd5084331b0b1942f01ac96d31a0 Mon Sep 17 00:00:00 2001 From: Forbes Lindesay Date: Mon, 18 Feb 2019 14:02:44 +0000 Subject: [PATCH 2/2] Add missing test case output --- tests/baselines/reference/typeGuardBoolean.js | 30 +++++++++++++ .../reference/typeGuardBoolean.symbols | 35 +++++++++++++++ .../reference/typeGuardBoolean.types | 44 +++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 tests/baselines/reference/typeGuardBoolean.js create mode 100644 tests/baselines/reference/typeGuardBoolean.symbols create mode 100644 tests/baselines/reference/typeGuardBoolean.types diff --git a/tests/baselines/reference/typeGuardBoolean.js b/tests/baselines/reference/typeGuardBoolean.js new file mode 100644 index 0000000000000..1bfe41983d83b --- /dev/null +++ b/tests/baselines/reference/typeGuardBoolean.js @@ -0,0 +1,30 @@ +//// [typeGuardBoolean.ts] +function test(strOrNull: string | null, strOrUndefined: string | undefined) { + var str: string = "original"; + var nil: null; + if (!Boolean(strOrNull)) { + nil = strOrNull; + } + else { + str = strOrNull; + } + if (Boolean(strOrUndefined)) { + str = strOrUndefined; + } +} + + +//// [typeGuardBoolean.js] +function test(strOrNull, strOrUndefined) { + var str = "original"; + var nil; + if (!Boolean(strOrNull)) { + nil = strOrNull; + } + else { + str = strOrNull; + } + if (Boolean(strOrUndefined)) { + str = strOrUndefined; + } +} diff --git a/tests/baselines/reference/typeGuardBoolean.symbols b/tests/baselines/reference/typeGuardBoolean.symbols new file mode 100644 index 0000000000000..6a08dcbda8a06 --- /dev/null +++ b/tests/baselines/reference/typeGuardBoolean.symbols @@ -0,0 +1,35 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardBoolean.ts === +function test(strOrNull: string | null, strOrUndefined: string | undefined) { +>test : Symbol(test, Decl(typeGuardBoolean.ts, 0, 0)) +>strOrNull : Symbol(strOrNull, Decl(typeGuardBoolean.ts, 0, 14)) +>strOrUndefined : Symbol(strOrUndefined, Decl(typeGuardBoolean.ts, 0, 39)) + + var str: string = "original"; +>str : Symbol(str, Decl(typeGuardBoolean.ts, 1, 5)) + + var nil: null; +>nil : Symbol(nil, Decl(typeGuardBoolean.ts, 2, 5)) + + if (!Boolean(strOrNull)) { +>Boolean : Symbol(Boolean, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>strOrNull : Symbol(strOrNull, Decl(typeGuardBoolean.ts, 0, 14)) + + nil = strOrNull; +>nil : Symbol(nil, Decl(typeGuardBoolean.ts, 2, 5)) +>strOrNull : Symbol(strOrNull, Decl(typeGuardBoolean.ts, 0, 14)) + } + else { + str = strOrNull; +>str : Symbol(str, Decl(typeGuardBoolean.ts, 1, 5)) +>strOrNull : Symbol(strOrNull, Decl(typeGuardBoolean.ts, 0, 14)) + } + if (Boolean(strOrUndefined)) { +>Boolean : Symbol(Boolean, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>strOrUndefined : Symbol(strOrUndefined, Decl(typeGuardBoolean.ts, 0, 39)) + + str = strOrUndefined; +>str : Symbol(str, Decl(typeGuardBoolean.ts, 1, 5)) +>strOrUndefined : Symbol(strOrUndefined, Decl(typeGuardBoolean.ts, 0, 39)) + } +} + diff --git a/tests/baselines/reference/typeGuardBoolean.types b/tests/baselines/reference/typeGuardBoolean.types new file mode 100644 index 0000000000000..db3d6f39cd841 --- /dev/null +++ b/tests/baselines/reference/typeGuardBoolean.types @@ -0,0 +1,44 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardBoolean.ts === +function test(strOrNull: string | null, strOrUndefined: string | undefined) { +>test : (strOrNull: string | null, strOrUndefined: string | undefined) => void +>strOrNull : string | null +>null : null +>strOrUndefined : string | undefined + + var str: string = "original"; +>str : string +>"original" : "original" + + var nil: null; +>nil : null +>null : null + + if (!Boolean(strOrNull)) { +>!Boolean(strOrNull) : boolean +>Boolean(strOrNull) : boolean +>Boolean : BooleanConstructor +>strOrNull : string | null + + nil = strOrNull; +>nil = strOrNull : null +>nil : null +>strOrNull : null + } + else { + str = strOrNull; +>str = strOrNull : string +>str : string +>strOrNull : string + } + if (Boolean(strOrUndefined)) { +>Boolean(strOrUndefined) : boolean +>Boolean : BooleanConstructor +>strOrUndefined : string | undefined + + str = strOrUndefined; +>str = strOrUndefined : string +>str : string +>strOrUndefined : string + } +} +