Skip to content

Commit

Permalink
add type guard for is.truthy to narrow result
Browse files Browse the repository at this point in the history
  • Loading branch information
zshannon authored Jan 16, 2022
1 parent f5cc764 commit dfd6d4e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
5 changes: 2 additions & 3 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,9 +247,8 @@ 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;

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


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

0 comments on commit dfd6d4e

Please sign in to comment.