Skip to content

Commit 4f99901

Browse files
authored
optimize value type infer for Validatable (#67)
1 parent 0f9df8a commit 4f99901

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/types.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Validatable, ValueOf } from './types'
2+
3+
// https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
4+
export type Equal<X, Y> = (
5+
(<T>() => T extends X ? 1 : 2) extends
6+
(<T>() => T extends Y ? 1 : 2)
7+
? true
8+
: false
9+
)
10+
11+
function assertEqual<T, S>(..._args: Equal<T, S> extends true ? [] : [never]) {}
12+
13+
describe('ValueOf', () => {
14+
it('should work well with Validatable', () => {
15+
const a = {} as Validatable<string>
16+
assertEqual<ValueOf<typeof a>, string>()
17+
})
18+
})

src/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,15 @@ export type ValueOfFields<Fields> = (
9292
export type ValueOf<State> = (
9393
State extends FormState<infer Fields>
9494
? ValueOfFields<Fields>
95-
: ValueOfFieldState<State>
95+
: (
96+
State extends FieldState<infer V>
97+
? V
98+
: (
99+
State extends Validatable<unknown, infer V>
100+
? V
101+
: never
102+
)
103+
)
96104
)
97105

98106
/** Validate status. */

0 commit comments

Comments
 (0)