由于TypeScript是强类型的,简单地使用if(){}来检查null和undefined听起来并不正确。
TypeScript有专门的函数或语法吗?
由于TypeScript是强类型的,简单地使用if(){}来检查null和undefined听起来并不正确。
TypeScript有专门的函数或语法吗?
当前回答
如果你正在使用TypeScript,让编译器检查空值和未定义值(或可能存在的)是一个更好的方法,而不是在运行时检查它们。(如果您确实想在运行时检查,那么正如许多答案所表明的那样,只需使用value == null)。
使用编译选项strictNullChecks告诉编译器阻塞可能的空值或未定义值。如果你设置了这个选项,然后有一个你想要允许null和undefined的情况,你可以定义类型为type | null | undefined。
其他回答
if( value ) {
}
如果value不为true,则求值为true:
零 未定义的 南 空字符串" 0 假
Typescript包含javascript规则。
试试这个,用!!运算符和变量。
let check;
if (!!check) {
console.log('check is not null or not undefined');
} else {
console.log('check is null or undefined');
}
它在Angular中非常有用。 检查任何变量的undefined和null。
你可能想试试
if(!!someValue)
! !
解释
第一个!将表达式转换为布尔值。
如果someValue为假则为真,如果someValue为真则为假。这可能会让人困惑。
通过添加另一个!,表达式现在如果someValue为真则为真,如果someValue为假则为假,这更容易管理。
讨论
现在,为什么我要用if (!!someValue)来麻烦自己,而像if (someValue)这样的东西会给我相同的结果?
因为! !someValue恰好是一个布尔表达式,而someValue可以是任何东西。这种表达式现在可以编写如下函数(上帝,我们需要这样的函数):
isSomeValueDefined(): boolean {
return !!someValue
}
而不是:
isSomeValueDefined(): boolean {
if(someValue) {
return true
}
return false
}
我希望这能有所帮助。
晚加入这个线程,但我发现这个JavaScript黑客在检查一个值是否未定义非常方便
if(typeof(something) === 'undefined'){
// Yes this is undefined
}
如果你使用本地存储,小心,你可能会以字符串undefined而不是值undefined结束:
localStorage.setItem('mykey',JSON.stringify(undefined));
localStorage.getItem('mykey') === "undefined"
true
人们可能会发现这个很有用:https://github.com/angular/components/blob/master/src/cdk/coercion/boolean-property.spec.ts
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/** Coerces a data-bound value (typically a string) to a boolean. */
export function coerceBooleanProperty(value: any): boolean {
return value != null && `${value}` !== 'false';
}
import {coerceBooleanProperty} from './boolean-property';
describe('coerceBooleanProperty', () => {
it('should coerce undefined to false', () => {
expect(coerceBooleanProperty(undefined)).toBe(false);
});
it('should coerce null to false', () => {
expect(coerceBooleanProperty(null)).toBe(false);
});
it('should coerce the empty string to true', () => {
expect(coerceBooleanProperty('')).toBe(true);
});
it('should coerce zero to true', () => {
expect(coerceBooleanProperty(0)).toBe(true);
});
it('should coerce the string "false" to false', () => {
expect(coerceBooleanProperty('false')).toBe(false);
});
it('should coerce the boolean false to false', () => {
expect(coerceBooleanProperty(false)).toBe(false);
});
it('should coerce the boolean true to true', () => {
expect(coerceBooleanProperty(true)).toBe(true);
});
it('should coerce the string "true" to true', () => {
expect(coerceBooleanProperty('true')).toBe(true);
});
it('should coerce an arbitrary string to true', () => {
expect(coerceBooleanProperty('pink')).toBe(true);
});
it('should coerce an object to true', () => {
expect(coerceBooleanProperty({})).toBe(true);
});
it('should coerce an array to true', () => {
expect(coerceBooleanProperty([])).toBe(true);
});
});