Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -247,11 +247,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);

Expand Down
2 changes: 2 additions & 0 deletions source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ export interface ObservableLike {
subscribe(observer: (value: unknown) => void): void;
[Symbol.observable](): ObservableLike;
}

export type Falsy = false | 0 | 0n | '' | null | undefined;