Skip to content

Commit

Permalink
fix(isNil): Accept generic type for inversion type extraction (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
Harris-Miller authored Jan 31, 2024
1 parent 4d4f2ea commit dbacd7a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
26 changes: 26 additions & 0 deletions test/isNil.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expectType } from 'tsd';
import { filter, isNil } from '../es';

expectType<boolean>(isNil(1));
expectType<boolean>(isNil('a'));
expectType<boolean>(isNil(true));

expectType<boolean>(isNil(null));
expectType<boolean>(isNil(undefined));

const maybeNumber = (): number | null => {
if (Math.random() > 0.5) {
return null;
}
return 123;
};

expectType<number | null>(maybeNumber());

const r = maybeNumber();

if (isNil(r)) {
expectType<null>(r);
}

expectType<undefined[]>(filter(isNil, [] as (string | undefined)[]));
4 changes: 3 additions & 1 deletion test/isNotNil.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectType } from 'tsd';
import { isNotNil } from '../es';
import { filter, isNotNil } from '../es';

expectType<boolean>(isNotNil(1));
expectType<boolean>(isNotNil('a'));
Expand All @@ -22,3 +22,5 @@ const r = maybeNumber();
if (isNotNil(r)) {
expectType<number>(r);
}

expectType<string[]>(filter(isNotNil, [] as (string | undefined)[]));
2 changes: 1 addition & 1 deletion types/isNil.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export function isNil(value: any): value is null | undefined;
export function isNil<T>(value: T): value is ((null | undefined) & T);

0 comments on commit dbacd7a

Please sign in to comment.