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

Feat is never #115

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,23 @@ test('Conditions can be based on XOR', t => {

### IsNever


```ts
test('Can ask if T is of type "never"', t => {
assert<IsNever<'hi'>, False>(t);
assert<IsNever<any>, False>(t);
assert<IsNever<object>, False>(t);
assert<IsNever<Function>, False>(t);
assert<IsNever<() => string>, False>(t);
assert<IsNever<number>, False>(t);
assert<IsNever<boolean>, False>(t);
assert<IsNever<string | number>, False>(t);
assert<IsNever<unknown>, False>(t);
assert<IsNever<null>, False>(t);
assert<IsNever<undefined>, False>(t);

assert<IsNever<never>, True>(t);
});
```

### IsNil

Expand Down
2 changes: 1 addition & 1 deletion scripts/testTsVersions.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
set -e

for v in 3.0.3 3.1.6 3.2.2 next; do
for v in 3.5.3 next; do
npm install --no-save typescript@$v
npm test
done
2 changes: 1 addition & 1 deletion src/types/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type ObjectPrototypeKeys = keyof Object;
/** no-doc */
export type FunctionPrototypeKeys = keyof Function;

export type IsNever<S extends string> = Not<(Record<S, True> & Record<string, False>)[S]>;
export type IsNever<T> = keyof any extends keyof T ? Not<IsAny<T>> : False;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's weird to me that keyof never is the same as keyof any, while keyof unknown is never. This feels like the sort of thing that might break in a later version of TS, but I suppose that's what the tests are for...

export type IsType<T, X> = X extends T ? True : False;
export type IsArray<T> = T extends unknown[] ? True : False;
export type IsNumber<T> = T extends number ? True : False;
Expand Down
20 changes: 20 additions & 0 deletions test/predicates/IsNever.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import test from 'ava';
import { assert } from '../helpers/assert';

import { False, True, IsNever } from '../../src';

test('Can ask if T is of type "never"', t => {
assert<IsNever<'hi'>, False>(t);
assert<IsNever<any>, False>(t);
assert<IsNever<object>, False>(t);
assert<IsNever<Function>, False>(t);
assert<IsNever<() => string>, False>(t);
assert<IsNever<number>, False>(t);
assert<IsNever<boolean>, False>(t);
assert<IsNever<string | number>, False>(t);
assert<IsNever<unknown>, False>(t);
assert<IsNever<null>, False>(t);
assert<IsNever<undefined>, False>(t);

assert<IsNever<never>, True>(t);
});
11 changes: 0 additions & 11 deletions test/strings/IsNever.test.ts

This file was deleted.