Allow Boolean() to be used to perform a null check (#29955)
* Allow Boolean() to be used to perform a null check * Add missing test case output
This commit is contained in:
parent
be409fad84
commit
3ce3cde493
2
src/lib/es5.d.ts
vendored
2
src/lib/es5.d.ts
vendored
|
@ -513,7 +513,7 @@ interface Boolean {
|
||||||
|
|
||||||
interface BooleanConstructor {
|
interface BooleanConstructor {
|
||||||
new(value?: any): Boolean;
|
new(value?: any): Boolean;
|
||||||
(value?: any): boolean;
|
<T>(value?: T): value is Exclude<T, false | null | undefined | '' | 0>;
|
||||||
readonly prototype: Boolean;
|
readonly prototype: Boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
30
tests/baselines/reference/typeGuardBoolean.js
Normal file
30
tests/baselines/reference/typeGuardBoolean.js
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
35
tests/baselines/reference/typeGuardBoolean.symbols
Normal file
35
tests/baselines/reference/typeGuardBoolean.symbols
Normal file
|
@ -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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
44
tests/baselines/reference/typeGuardBoolean.types
Normal file
44
tests/baselines/reference/typeGuardBoolean.types
Normal file
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue