feat(typescript-types): Fully rewrite KeyPaths<T,O,F>
Rewrite KeyPaths to eleminate 'Too deep instantiations' error of TypeScript.
This commit is contained in:
@@ -18,10 +18,26 @@ The types included in this library are categorized by their purpose.
|
||||
[`BuiltIns`]: src/built-ins.ts
|
||||
[`NonContainerType`]: src/non-container-type.ts
|
||||
|
||||
#### Boolean Operator Types
|
||||
|
||||
| Type | Description |
|
||||
| --------------- | -------------------------------------------------------------------------- |
|
||||
| [`And<A, B>`][] | `true` if `A` and `B` (both extend `boolean`) are both `true`. |
|
||||
| [`Not<A>`][] | `true` if `A` (extends `boolean`) is `false` and vice-versa. |
|
||||
| [`Or<A>`][] | `true` if either `A` or `B` (both extend `boolean`) is `true`. |
|
||||
| [`Xor<A, B>`][] | `true` if `A` and `B` (both extend `boolean`) are not both `false`/`true`. |
|
||||
|
||||
[`And<A, B>`]: src/and.ts
|
||||
[`Not<A>`]: src/not.ts
|
||||
[`Or<A>`]: src/or.ts
|
||||
[`Xor<A, B>`]: src/xor.ts
|
||||
|
||||
#### Test Types
|
||||
|
||||
| Type | Description |
|
||||
| --------------------------------------- | --------------------------------------------------------------------------------------------------------- |
|
||||
| [`Extends<A, B>`][] | `true` if `A` extends `B`, `false` otherwise[^extends_remark]. |
|
||||
| [`ExtendsExactly<A, B>`][] | `true` if `A` extends `B`, `false` otherwise[^extends-exactly_remark]. |
|
||||
| [`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`) |
|
||||
| [`IsUndefined<T>`][] | `true` if `T` is `undefined`, `false` otherwise (`null`, `never`, `any` also yield `false`). |
|
||||
@@ -30,10 +46,12 @@ The types included in this library are categorized by their purpose.
|
||||
| [`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`.
|
||||
[^extends_remark]: The result may be `boolean` (aka `true | false`) if `A` or `B` is a union type and the union members evaluate to different boolean results.
|
||||
|
||||
[^is-empty-string_remark]: If `T` is `any` will yield `true`, as it is taken to be `any` string.
|
||||
[^extends-exactly_remark]: The extends check is done non-distributively in case `A` or `B` is a union type.
|
||||
|
||||
[`Extends<A, B>`]: src/extends.ts
|
||||
[`ExtendsExactly<A, B>`]: src/extends-exactly.ts
|
||||
[`IsAny<T>`]: src/is-any.ts
|
||||
[`IsNever<T>`]: src/is-never.ts
|
||||
[`IsUndefined<T>`]: src/is-undefined.ts
|
||||
@@ -70,8 +88,10 @@ The types included in this library are categorized by their purpose.
|
||||
|
||||
#### Combination Types
|
||||
|
||||
| Type | Description |
|
||||
| ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
|
||||
| [`Assign<Shape, Defaults, Obj>`][] | Return a type with the structure of `Shape` and value types from `Obj` or `Default` for missing optional keys in `Obj`. |
|
||||
| Type | Description |
|
||||
| --------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
|
||||
| [`Assign<Shape, Defaults, Obj>`][] | Return a type with the structure of `Shape` and value types from `Obj` or `Default` for missing optional keys in `Obj`. |
|
||||
| [`Concat<Prefix, Suffix, Separator>`][] | Concatenate `Prefix` and `Suffix` and - if both are non empty strings - separate them using `Separator`. |
|
||||
|
||||
[`Assign<Shape, Defaults, Obj>`]: src/assign.ts
|
||||
[`Concat<Prefix, Suffix, Separator>`]: src/concat.ts
|
||||
|
||||
5
packages/typescript-types/src/and.ts
Normal file
5
packages/typescript-types/src/and.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export type And<A extends boolean, B extends boolean> = A extends true
|
||||
? B extends true
|
||||
? true
|
||||
: false
|
||||
: false
|
||||
9
packages/typescript-types/src/concat.ts
Normal file
9
packages/typescript-types/src/concat.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { If } from './if.js'
|
||||
import type { Or } from './or.js'
|
||||
import type { IsEmptyString } from './is-empty-string.js'
|
||||
|
||||
export type Concat<Prefix extends string, Suffix extends string, Separator extends string = ''> = If<
|
||||
Or<IsEmptyString<Prefix>, IsEmptyString<Suffix>>,
|
||||
`${Prefix}${Suffix}`,
|
||||
`${Prefix}${Separator}${Suffix}`
|
||||
>
|
||||
1
packages/typescript-types/src/extends-exactly.ts
Normal file
1
packages/typescript-types/src/extends-exactly.ts
Normal file
@@ -0,0 +1 @@
|
||||
export type ExtendsExactly<A, B> = [A] extends [B] ? true : false
|
||||
1
packages/typescript-types/src/extends.ts
Normal file
1
packages/typescript-types/src/extends.ts
Normal file
@@ -0,0 +1 @@
|
||||
export type Extends<A, B> = A extends B ? true : false
|
||||
@@ -1,5 +1,8 @@
|
||||
export type * from './and.js'
|
||||
export type * from './assign.js'
|
||||
export type * from './built-ins.js'
|
||||
export type * from './extends-exactly.js'
|
||||
export type * from './extends.js'
|
||||
export type * from './get.js'
|
||||
export type * from './if.js'
|
||||
export type * from './is-any.js'
|
||||
@@ -14,3 +17,4 @@ export type * from './optional-keys-of.js'
|
||||
export type * from './pick-assignable.js'
|
||||
export type * from './primitive.js'
|
||||
export type * from './simplify.js'
|
||||
export type * from './xor.js'
|
||||
|
||||
@@ -1,55 +1,68 @@
|
||||
import type { If } from './if.js'
|
||||
import type { IsEmptyString } from './is-empty-string.js'
|
||||
import type { IsUndefined } from './is-undefined.js'
|
||||
import type { Simplify } from './simplify.js'
|
||||
import type { Extends } from './extends.js'
|
||||
import type { Concat } from './concat.js'
|
||||
import type { Assign } from './assign.js'
|
||||
import type { NonContainerType } from './non-container-type.js'
|
||||
|
||||
interface KeyPaths_Options {
|
||||
export interface KeyPaths_Options {
|
||||
separator?: string
|
||||
leavesOnly?: boolean
|
||||
invertFilter?: boolean
|
||||
}
|
||||
|
||||
type RequiredOptions = Required<KeyPaths_Options>
|
||||
|
||||
interface KeyPaths_DefaultOptions {
|
||||
separator: '.'
|
||||
leavesOnly: true
|
||||
invertFilter: false
|
||||
}
|
||||
|
||||
type GetPrefixedKey<
|
||||
Parent extends string,
|
||||
Key extends string,
|
||||
Separator extends string
|
||||
> = `${If<IsEmptyString<Parent>, '', `${Parent}${Separator}`>}${Key}`
|
||||
type ExtendsFilter<Obj, Key extends keyof Obj, Filter> =
|
||||
Extends<Obj[Key], Filter> extends false ? false : true
|
||||
|
||||
type PrefixIfNot<Cond, Prefix, T> = If<Cond, T, Prefix | T>
|
||||
type IncludeElement<Obj, Key extends keyof Obj, Filter> = Obj extends never
|
||||
? false
|
||||
: Obj[Key] extends never
|
||||
? false
|
||||
: unknown extends Obj[Key]
|
||||
? false
|
||||
: ExtendsFilter<Obj, Key, Filter> extends false
|
||||
? true
|
||||
: false
|
||||
|
||||
type KeyPathOf<Obj, Options extends Required<KeyPaths_Options>, Parent extends string, Filter = never> =
|
||||
Obj extends Record<PropertyKey, unknown>
|
||||
? PrefixIfNot<Options['leavesOnly'], Parent, KeyPathsOfStringKeys<Obj, Options, Filter, Parent>>
|
||||
: If<IsUndefined<Obj>, never, Parent>
|
||||
type AdaptKeyForOptionals<Obj, Key extends keyof Obj> = Key extends number | string
|
||||
? undefined extends Obj[Key]
|
||||
? Obj[Key] extends undefined
|
||||
? `${Key}`
|
||||
: `${Key}?`
|
||||
: `${Key}`
|
||||
: never
|
||||
|
||||
type NonUndefined<Obj> = Obj extends undefined ? never : Obj
|
||||
type KeysOf<Obj> = Obj extends undefined ? never : Exclude<keyof Obj, keyof any[]>
|
||||
|
||||
type KeyPathsOfStringKeys<
|
||||
Obj extends object,
|
||||
Options extends Required<KeyPaths_Options>,
|
||||
Obj,
|
||||
Key extends keyof Obj,
|
||||
Options extends RequiredOptions,
|
||||
Filter,
|
||||
Parent extends string = ''
|
||||
> = {
|
||||
[Key in keyof Obj & string]: Obj[Key] extends Filter
|
||||
? If<
|
||||
Options['invertFilter'],
|
||||
KeyPathOf<Obj[Key], Options, GetPrefixedKey<Parent, Key, Options['separator']>, Filter>,
|
||||
never
|
||||
>
|
||||
: If<
|
||||
Options['invertFilter'],
|
||||
never,
|
||||
KeyPathOf<Obj[Key], Options, GetPrefixedKey<Parent, Key, Options['separator']>, Filter>
|
||||
>
|
||||
}[keyof Obj & string]
|
||||
> = Key extends Key
|
||||
? IncludeElement<Obj, Key, Filter> extends true
|
||||
? Obj[Key] extends NonContainerType
|
||||
? Concat<Parent, AdaptKeyForOptionals<Obj, Key>, Options['separator']>
|
||||
: KeyPathsOfStringKeys<
|
||||
NonUndefined<Obj[Key]>,
|
||||
KeysOf<Obj[Key]>,
|
||||
Options,
|
||||
Filter,
|
||||
Concat<Parent, AdaptKeyForOptionals<Obj, Key>, Options['separator']>
|
||||
>
|
||||
: never
|
||||
: never
|
||||
|
||||
export type KeyPaths<
|
||||
Obj extends object,
|
||||
Obj,
|
||||
Options extends KeyPaths_Options = {},
|
||||
Filter = null | undefined
|
||||
> = Simplify<KeyPathsOfStringKeys<Obj, Assign<KeyPaths_Options, KeyPaths_DefaultOptions, Options>, Filter>>
|
||||
> = KeyPathsOfStringKeys<Obj, keyof Obj, Assign<KeyPaths_Options, KeyPaths_DefaultOptions, Options>, Filter>
|
||||
|
||||
1
packages/typescript-types/src/not.ts
Normal file
1
packages/typescript-types/src/not.ts
Normal file
@@ -0,0 +1 @@
|
||||
export type Not<A extends boolean> = A extends true ? false : true
|
||||
1
packages/typescript-types/src/or.ts
Normal file
1
packages/typescript-types/src/or.ts
Normal file
@@ -0,0 +1 @@
|
||||
export type Or<A extends boolean, B extends boolean> = A extends true ? true : B extends true ? true : false
|
||||
1
packages/typescript-types/src/xor.ts
Normal file
1
packages/typescript-types/src/xor.ts
Normal file
@@ -0,0 +1 @@
|
||||
export type Xor<A extends boolean, B extends Boolean> = [A] extends [B] ? false : true
|
||||
7
packages/typescript-types/test/and.tst.ts
Normal file
7
packages/typescript-types/test/and.tst.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import type { And } from '@/and.js'
|
||||
import { expect } from 'tstyche'
|
||||
|
||||
expect<And<true, true>>().type.toBe<true>()
|
||||
expect<And<true, false>>().type.toBe<false>()
|
||||
expect<And<false, true>>().type.toBe<false>()
|
||||
expect<And<false, false>>().type.toBe<false>()
|
||||
8
packages/typescript-types/test/concat.tst.ts
Normal file
8
packages/typescript-types/test/concat.tst.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import type { Concat } from '@/concat.js'
|
||||
import { expect } from 'tstyche'
|
||||
|
||||
expect<Concat<'', '', ''>>().type.toBe<''>()
|
||||
expect<Concat<'Parent', '', ''>>().type.toBe<'Parent'>()
|
||||
expect<Concat<'Parent', 'Child', ''>>().type.toBe<'ParentChild'>()
|
||||
expect<Concat<'Parent', 'Child', '.'>>().type.toBe<'Parent.Child'>()
|
||||
expect<Concat<'', 'Child', '.'>>().type.toBe<'Child'>()
|
||||
7
packages/typescript-types/test/extends-exactly.tst.ts
Normal file
7
packages/typescript-types/test/extends-exactly.tst.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import type { ExtendsExactly } from '@/extends-exactly.js'
|
||||
import { expect } from 'tstyche'
|
||||
|
||||
expect<ExtendsExactly<true, true>>().type.toBe<true>()
|
||||
expect<ExtendsExactly<true, boolean>>().type.toBe<true>()
|
||||
expect<ExtendsExactly<boolean, true>>().type.toBe<false>()
|
||||
expect<ExtendsExactly<false, true>>().type.toBe<false>()
|
||||
10
packages/typescript-types/test/extends.tst.ts
Normal file
10
packages/typescript-types/test/extends.tst.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import type { Extends } from '@/extends.js'
|
||||
import { expect } from 'tstyche'
|
||||
|
||||
expect<Extends<true, true>>().type.toBe<true>()
|
||||
expect<Extends<true, boolean>>().type.toBe<true>()
|
||||
expect<Extends<boolean, true>>().type.toBe<boolean>()
|
||||
expect<Extends<false, true>>().type.toBe<false>()
|
||||
expect<Extends<false, never>>().type.toBe<false>()
|
||||
expect<Extends<null, never>>().type.toBe<false>()
|
||||
expect<Extends<undefined, never>>().type.toBe<false>()
|
||||
@@ -4,50 +4,31 @@ import { expect } from 'tstyche'
|
||||
interface ExampleObject {
|
||||
nullvalue: null
|
||||
simplevalue: string
|
||||
optionalKey?: {
|
||||
nullvalue: null
|
||||
simplevalue: string
|
||||
}
|
||||
unknownKey: unknown
|
||||
neverKey: never
|
||||
config: {
|
||||
nullvalue: null
|
||||
simplevalue: string
|
||||
}
|
||||
undefinedKey: undefined
|
||||
undefinedOptionalKey?: undefined
|
||||
optionalKey?: string
|
||||
arrayKey: [{ name: string; age: number }, 1]
|
||||
optionalArrayKey?: [{ name: string }, { age?: number }]
|
||||
}
|
||||
|
||||
type AllKeys =
|
||||
type KeysWhenFilterIsSetToNever =
|
||||
| 'nullvalue'
|
||||
| 'simplevalue'
|
||||
| 'optionalKey.nullvalue'
|
||||
| 'optionalKey.simplevalue'
|
||||
| 'unknownKey'
|
||||
| 'config.nullvalue'
|
||||
| 'config.simplevalue'
|
||||
| 'undefinedKey'
|
||||
| 'undefinedOptionalKey'
|
||||
| 'optionalKey?'
|
||||
| 'arrayKey.0.name'
|
||||
| 'arrayKey.0.age'
|
||||
| 'arrayKey.1'
|
||||
| 'optionalArrayKey?.0.name'
|
||||
| 'optionalArrayKey?.1.age?'
|
||||
|
||||
// type T = any extends never ? true : false
|
||||
// type A = KeyPaths<ExampleObject, {}, never>
|
||||
expect<KeyPaths<ExampleObject>>().type.toBe<
|
||||
'simplevalue' | 'optionalKey.simplevalue' | 'unknownKey' | 'config.simplevalue'
|
||||
'simplevalue' | 'arrayKey.0.name' | 'arrayKey.0.age' | 'arrayKey.1'
|
||||
>()
|
||||
expect<KeyPaths<ExampleObject, {}, any>>().type.toBeAssignableTo<never>()
|
||||
expect<KeyPaths<ExampleObject, {}, unknown>>().type.toBeAssignableTo<never>()
|
||||
expect<KeyPaths<ExampleObject, {}, never>>().type.toBe<AllKeys>()
|
||||
expect<KeyPaths<ExampleObject, { invertFilter: true }, any>>().type.toBe<AllKeys>()
|
||||
expect<KeyPaths<ExampleObject, {}, null>>().type.toBe<
|
||||
'simplevalue' | 'optionalKey.simplevalue' | 'unknownKey' | 'config.simplevalue'
|
||||
>()
|
||||
expect<KeyPaths<ExampleObject, {}, string>>().type.toBe<
|
||||
'nullvalue' | 'optionalKey.nullvalue' | 'unknownKey' | 'config.nullvalue'
|
||||
>()
|
||||
expect<KeyPaths<ExampleObject, { separator: '-' }>>().type.toBe<
|
||||
'simplevalue' | 'optionalKey-simplevalue' | 'unknownKey' | 'config-simplevalue'
|
||||
>()
|
||||
expect<KeyPaths<ExampleObject, { leavesOnly: false }>>().type.toBe<
|
||||
'simplevalue' | 'optionalKey' | 'optionalKey.simplevalue' | 'unknownKey' | 'config' | 'config.simplevalue'
|
||||
>()
|
||||
|
||||
expect<KeyPaths<any, {}, any>>().type.toBeAssignableTo<never>()
|
||||
expect<KeyPaths<never, {}, any>>().type.toBeAssignableTo<never>()
|
||||
expect<KeyPaths<never, {}, never>>().type.toBeAssignableTo<never>()
|
||||
|
||||
expect<KeyPaths<any, {}, never>>().type.toBe<unknown>()
|
||||
expect<KeyPaths<any, {}, string>>().type.toBeAssignableTo<unknown>()
|
||||
expect<KeyPaths<ExampleObject, {}, never>>().type.toBe<KeysWhenFilterIsSetToNever>()
|
||||
expect<KeyPaths<ExampleObject, {}, any>>().type.toBeAssignableWith<never>()
|
||||
|
||||
5
packages/typescript-types/test/not.tst.ts
Normal file
5
packages/typescript-types/test/not.tst.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import type { Not } from '@/not.js'
|
||||
import { expect } from 'tstyche'
|
||||
|
||||
expect<Not<true>>().type.toBe<false>()
|
||||
expect<Not<false>>().type.toBe<true>()
|
||||
7
packages/typescript-types/test/or.tst.ts
Normal file
7
packages/typescript-types/test/or.tst.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import type { Or } from '@/or.js'
|
||||
import { expect } from 'tstyche'
|
||||
|
||||
expect<Or<true, true>>().type.toBe<true>()
|
||||
expect<Or<true, false>>().type.toBe<true>()
|
||||
expect<Or<false, true>>().type.toBe<true>()
|
||||
expect<Or<false, false>>().type.toBe<false>()
|
||||
7
packages/typescript-types/test/xor.tst.ts
Normal file
7
packages/typescript-types/test/xor.tst.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import type { Xor } from '@/xor.js'
|
||||
import { expect } from 'tstyche'
|
||||
|
||||
expect<Xor<true, true>>().type.toBe<false>()
|
||||
expect<Xor<true, false>>().type.toBe<true>()
|
||||
expect<Xor<false, true>>().type.toBe<true>()
|
||||
expect<Xor<false, false>>().type.toBe<false>()
|
||||
Reference in New Issue
Block a user