-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add type checking to Lens (flat paths) — WIP #1
- Loading branch information
1 parent
91ba908
commit 9759b2a
Showing
4 changed files
with
134 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { TypesMap, Generic, unwrap } from 'free-types-core'; | ||
import { Fn, Param, Output } from './types' | ||
import { Prev, Next } from './utils'; | ||
import { Lens } from './Lens' | ||
import { FollowPath, NOT_FOUND } from './Follow'; | ||
|
||
export type Audit< | ||
L extends Lens, | ||
Model, | ||
I extends number = 0, | ||
F = FollowPath<L['path'][I], Model, Model> | ||
> = F extends NOT_FOUND ? [...LastPathItem<L, I>, NextPathItem<Model>] | ||
: Next<I> extends L['path']['length'] ? F | ||
: Audit<L, F, Next<I>>; | ||
|
||
type LastPathItem<L extends Lens, I extends number> = | ||
I extends 0 ? [] : [L['path'][Prev<I>]]; | ||
|
||
type NextPathItem<Model> = | ||
readonly any[] extends Model ? number | ||
: Model extends readonly unknown[] ? NumericArgs<Model> | ||
: Model extends Fn ? Output | Param<SequenceTo<Prev<Parameters<Model>['length']>>> | ||
: Model extends GenericFree ? TypesMap[unwrap<Model>['URI']] | ||
: Model extends Record<PropertyKey, unknown> ? { [K in keyof Model]: K }[keyof Model] | ||
: never | ||
|
||
type NumericArgs<T> = ToNumber<keyof T & `${number}`> | ||
|
||
type ToNumber<T> = T extends `${infer I extends number}` ? I : never | ||
|
||
type GenericFree = Exclude< | ||
Generic<TypesMap[keyof TypesMap]>, | ||
Fn | readonly unknown[] | Record<PropertyKey, unknown> | ||
>; | ||
|
||
type SequenceTo<N extends number, I extends number = 0, R = never> = | ||
I extends N ? R | I | ||
: SequenceTo<N, Next<I>, R | I> | ||
|
||
type Parameters<F, P = | ||
F extends { | ||
(...args: infer A): any | ||
(...args: infer B): any | ||
(...args: infer C): any | ||
(...args: infer D): any | ||
(...args: infer E): any | ||
(...args: infer F): any | ||
} ? A | B | C | D | E | F | ||
|
||
: F extends { | ||
(...args: infer A): any | ||
(...args: infer B): any | ||
(...args: infer C): any | ||
(...args: infer D): any | ||
(...args: infer E): any | ||
} ? A | B | C | D | E | ||
|
||
: F extends { | ||
(...args: infer A): any | ||
(...args: infer B): any | ||
(...args: infer C): any | ||
(...args: infer D): any | ||
} ? A | B | C | D | ||
|
||
: F extends { | ||
(...args: infer A): any | ||
(...args: infer B): any | ||
(...args: infer C): any | ||
} ? A | B | C | ||
|
||
: F extends { | ||
(...args: infer A): any | ||
(...args: infer B): any | ||
} ? A | B | ||
|
||
: F extends { | ||
(...args: infer A): any | ||
} ? A | ||
|
||
: never | ||
> = P extends any ? unknown[] extends P ? never : P : never; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1 @@ | ||
export { Next, Prev, Last, Assert }; | ||
|
||
type Prev<I extends number> = _prev[I]; | ||
type Next<I extends number> = _next[I]; | ||
|
||
type Last<T extends unknown[]> = T[Prev<T['length']>] | ||
|
||
type _prev = [never, 0,..._next]; | ||
type _next = [ | ||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, | ||
11,12,13,14,15,16,17,18,19,20, | ||
21,22,23,24,25,26,27,28,29,30, | ||
31,32,33,34,35,36,37,38,39,40, | ||
41,42,43,44,45,46,47,48,49,50, | ||
51,52,53,54,55,56,57,58,59,60, | ||
61,62,63,64 | ||
]; | ||
|
||
type Assert<T, U> = T extends U ? T : never | ||
export { Next, Prev, Last } from 'free-types-core/dist/utils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters