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

@@ -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>()