feat(typescript-types): Add IsEmptyKey<S>

This commit is contained in:
T. R. Bernstein
2025-07-14 22:09:36 +02:00
parent bb706e7780
commit 0cbc6d41a4
3 changed files with 12 additions and 0 deletions

View File

@@ -14,9 +14,12 @@ The types included in this library are categorized by their purpose.
| [`IsNever<T>`][] | `true` if `T` is `never`, `false` otherwise (`null`, `undefined`, `any` also yield `false`) |
| [`If<Test, TrueBranch, FalseBranch>`][] | Returns `TrueBranch` if `Test` is `true`, `FalseBranch` otherwise[^if_remark]. |
| [`IsKeyOf<T, K>`][] | `true` if `K` is a key of `T`, `false` otherwise. If `T` is `any`, any `K` but `never` will yield `true`. |
| [`IsEmptyString<S>`][] | `true` if `S` is the empty string `''`, `false` otherwise.[^is-empty-string_remark] |
[^if_remark]: If `boolean` is passed as `Test` the return value is a union of both branches, i.e. `TrueBranch | FalseBranch`.
[^is-empty-string_remark]: If `T` is `any` will yield `true`, as it is taken to be `any` string.
[`IsAny<T>`]: src/is-any.ts
[`IsNever<T>`]: src/is-never.ts
[`If<Test, TrueBranch, FalseBranch>`]: src/if.ts

View File

@@ -0,0 +1 @@
export type IsEmptyString<T extends string> = '' extends T ? true : false

View File

@@ -0,0 +1,8 @@
import type { IsEmptyString } from '@/is-empty-string.js'
import { expect } from 'tstyche'
expect<IsEmptyString<''>>().type.toBe<true>()
expect<IsEmptyString<any>>().type.toBe<true>()
expect<IsEmptyString<'nonempty'>>().type.toBe<false>()
expect<IsEmptyString<never>>().type.toBe<false>()