feat(typescript-types): Add IsNever<T>
This commit is contained in:
@@ -8,8 +8,10 @@ The types included in this library are categorized by their purpose.
|
||||
|
||||
#### Test Types
|
||||
|
||||
| Type | Description |
|
||||
| -------------- | ---------------------------------------------------------------------------------- |
|
||||
| [`IsAny<T>`][] | `true` if `T` is `any`, `false` otherwise (`null`, `undefined` also yield `false`) |
|
||||
| Type | Description |
|
||||
| ---------------- | ------------------------------------------------------------------------------------------- |
|
||||
| [`IsAny<T>`][] | `true` if `T` is `any`, `false` otherwise (`null`, `undefined` also yield `false`) |
|
||||
| [`IsNever<T>`][] | `true` if `T` is `never`, `false` otherwise (`null`, `undefined`, `any` also yield `false`) |
|
||||
|
||||
[`IsAny<T>`]: src/is-any.ts
|
||||
[`IsNever<T>`]: src/is-never.ts
|
||||
|
||||
4
packages/typescript-types/src/is-never.ts
Normal file
4
packages/typescript-types/src/is-never.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
// See: https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
|
||||
// https://stackoverflow.com/questions/53984650/typescript-never-type-inconsistently-matched-in-conditional-type/53984913#53984913
|
||||
// https://www.zhenghao.io/posts/ts-never
|
||||
export type IsNever<T> = [T] extends [never] ? true : false
|
||||
11
packages/typescript-types/test/is-never.tst.ts
Normal file
11
packages/typescript-types/test/is-never.tst.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import type { IsNever } from '@/is-never.js'
|
||||
import { expect } from 'tstyche'
|
||||
|
||||
expect<IsNever<never>>().type.toBe<true>()
|
||||
|
||||
expect<IsNever<1>>().type.toBe<false>()
|
||||
expect<IsNever<'somestring'>>().type.toBe<false>()
|
||||
expect<IsNever<null>>().type.toBe<false>()
|
||||
expect<IsNever<undefined>>().type.toBe<false>()
|
||||
expect<IsNever<unknown>>().type.toBe<false>()
|
||||
expect<IsNever<any>>().type.toBe<false>()
|
||||
Reference in New Issue
Block a user