feat(typescript-types): Add isKeyOf<T,K>

This commit is contained in:
T. R. Bernstein
2025-07-14 15:39:53 +02:00
parent a7bc60fa69
commit bb706e7780
3 changed files with 42 additions and 5 deletions

View File

@@ -8,11 +8,12 @@ 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`) |
| [`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]. |
| 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`) |
| [`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`. |
[^if_remark]: If `boolean` is passed as `Test` the return value is a union of both branches, i.e. `TrueBranch | FalseBranch`.

View File

@@ -0,0 +1,4 @@
import type { If } from './if.js'
import type { IsNever } from './is-never.js'
export type IsKeyOf<T, K> = If<IsNever<T>, false, If<IsNever<K>, false, K extends keyof T ? true : false>>

View File

@@ -0,0 +1,32 @@
import type { IsKeyOf } from '@/is-key-of.js'
import { expect } from 'tstyche'
interface Example1 {
requiredKey: number
optionalKey?: number
unknownNonOptionalKey: unknown
undefinedNonOptionalKey: undefined
neverKey: never
}
expect<IsKeyOf<Example1, 'requiredKey'>>().type.toBe<true>()
expect<IsKeyOf<Example1, 'optionalKey'>>().type.toBe<true>()
expect<IsKeyOf<Example1, 'unknownNonOptionalKey'>>().type.toBe<true>()
expect<IsKeyOf<Example1, 'undefinedNonOptionalKey'>>().type.toBe<true>()
expect<IsKeyOf<Example1, 'neverKey'>>().type.toBe<true>()
expect<IsKeyOf<string, 'toString'>>().type.toBe<true>()
expect<IsKeyOf<any, 123>>().type.toBe<true>()
expect<IsKeyOf<any, 'asdf'>>().type.toBe<true>()
expect<IsKeyOf<Example1, 'unknown'>>().type.toBe<false>()
expect<IsKeyOf<object, 'toString'>>().type.toBe<false>()
expect<IsKeyOf<Example1, never>>().type.toBe<false>()
expect<IsKeyOf<never, 'toString'>>().type.toBe<false>()
expect<IsKeyOf<never, 'anystring'>>().type.toBe<false>()
expect<IsKeyOf<never, 123>>().type.toBe<false>()
expect<IsKeyOf<any, never>>().type.toBe<false>()
expect<IsKeyOf<undefined, 123>>().type.toBe<false>()
expect<IsKeyOf<Example1, any>>().type.toBe<boolean>()