Add type guard for is.truthy and is.falsy (#151)

This commit is contained in:
Zane Shannon 2022-01-25 02:28:33 -08:00 committed by GitHub
parent 73daee6648
commit 63d75d68ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 4 deletions

View file

@ -2,7 +2,7 @@
/// <reference lib="dom"/>
/// <reference types="node"/>
import {Class, TypedArray, ObservableLike, Primitive} from './types';
import {Class, Falsy, TypedArray, ObservableLike, Primitive} from './types';
const typedArrayTypeNames = [
'Int8Array',
@ -248,11 +248,10 @@ is.urlString = (value: unknown): value is string => {
}
};
// TODO: Use the `not` operator with a type guard here when it's available.
// Example: `is.truthy = (value: unknown): value is (not false | not 0 | not '' | not undefined | not null) => Boolean(value);`
is.truthy = (value: unknown) => Boolean(value);
is.truthy = <T>(value: T | Falsy): value is T => Boolean(value);
// Example: `is.falsy = (value: unknown): value is (not true | 0 | '' | undefined | null) => Boolean(value);`
is.falsy = (value: unknown) => !value;
is.falsy = <T>(value: T | Falsy): value is Falsy => !value;
is.nan = (value: unknown) => Number.isNaN(value as number);

View file

@ -47,3 +47,5 @@ export interface ObservableLike {
subscribe(observer: (value: unknown) => void): void;
[Symbol.observable](): ObservableLike;
}
export type Falsy = false | 0 | 0n | '' | null | undefined;