Skip to content

Commit

Permalink
feat: πŸš€ isNever (#251)
Browse files Browse the repository at this point in the history
* feat: πŸš€ export IsUnknown

* docs: πŸ“„ changeset

* test: πŸ§ͺ testIsUnknown

* feat: βž• add IsNever

* test: πŸ§ͺ testIsNever

* docs: πŸ“„ changeset

* docs: βž• add IsUnknown πŸ“„

* docs: βž• add IsNever to README.md πŸ“„

* fix: πŸ§ͺ typo: never => unknown

* docs: πŸ§ͺ – => -
  • Loading branch information
Beraliv authored Sep 14, 2021
1 parent e39426b commit 6d10f69
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-olives-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ts-essentials": minor
---

Add `IsNever` which returns true if it's `never`, otherwise false
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ If you use any [functions](https://github.com/krzkaczor/ts-essentials/blob/maste
- [Dictionaries](#Dictionaries)
- [Type checkers](#type-checkers)
- `IsUnknown`
- `IsNever`
- [Deep\* wrapper types](#Deep-wrapper-types)
- DeepPartial
- DeepRequired
Expand Down Expand Up @@ -147,6 +148,15 @@ type Test1 = IsUnknown<unknown>;
type Test2 = IsUnknown<{ name: "Alexey" }>;
```

- `IsNever` checks whether we get `never` or not. If so, we get `true`. Otherwise, `false`

```typescript
// βœ… true
type Test1 = IsNever<never>;
// ❌ false
type Test2 = IsNever<{ name: "Alexey" }>;
```

### Deep\* wrapper types

- DeepPartial
Expand Down
1 change: 1 addition & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type IsTuple<T> = T extends any[] ? (any[] extends T ? never : T) : never
type AnyRecord<T = any> = Record<PropertyKey, T>;
// https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360
type IsAny<T> = 0 extends 1 & T ? true : false;
export type IsNever<T> = [T] extends [never] ? true : false;
export type IsUnknown<T> = IsAny<T> extends true ? false : unknown extends T ? true : false;
export type AnyArray<T = any> = Array<T> | ReadonlyArray<T>;

Expand Down
33 changes: 33 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
OmitProperties,
isExact,
IsUnknown,
IsNever,
} from "../lib";

function testDictionary() {
Expand Down Expand Up @@ -1292,3 +1293,35 @@ function testIsUnknown() {
Assert<IsExact<IsUnknown<any>, false>>,
];
}

function testIsNever() {
type cases = [
Assert<IsExact<IsNever<string>, false>>,
Assert<IsExact<IsNever<number>, false>>,
Assert<IsExact<IsNever<boolean>, false>>,
Assert<IsExact<IsNever<bigint>, false>>,
Assert<IsExact<IsNever<symbol>, false>>,
Assert<IsExact<IsNever<undefined>, false>>,
Assert<IsExact<IsNever<null>, false>>,
Assert<IsExact<IsNever<Function>, false>>,
Assert<IsExact<IsNever<Date>, false>>,
Assert<IsExact<IsNever<Error>, false>>,
Assert<IsExact<IsNever<RegExp>, false>>,
Assert<IsExact<IsNever<Map<string, unknown>>, false>>,
Assert<IsExact<IsNever<ReadonlyMap<string, unknown>>, false>>,
Assert<IsExact<IsNever<WeakMap<{ a: 1 }, unknown>>, false>>,
Assert<IsExact<IsNever<Set<string>>, false>>,
Assert<IsExact<IsNever<ReadonlySet<string>>, false>>,
Assert<IsExact<IsNever<WeakSet<{ a: 1 }>>, false>>,
Assert<IsExact<IsNever<{ a: 1 }>, false>>,
Assert<IsExact<IsNever<[]>, false>>,
Assert<IsExact<IsNever<[1]>, false>>,
Assert<IsExact<IsNever<readonly [1]>, false>>,
Assert<IsExact<IsNever<readonly number[]>, false>>,
Assert<IsExact<IsNever<number[]>, false>>,
Assert<IsExact<IsNever<Promise<number>>, false>>,
Assert<IsExact<IsNever<unknown>, false>>,
Assert<IsExact<IsNever<never>, true>>,
Assert<IsExact<IsNever<any>, false>>,
];
}

0 comments on commit 6d10f69

Please sign in to comment.