Skip to content

Commit

Permalink
fix: infer types (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcikado authored Nov 5, 2024
1 parent 00ac98e commit 54f5237
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 77 deletions.
20 changes: 13 additions & 7 deletions src/schema/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ import { group } from './object/group_builder.js'
import { VineNativeEnum } from './enum/native_enum.js'
import { VineUnionOfTypes } from './union_of_types/main.js'
import { ITYPE, OTYPE, COTYPE, IS_OF_TYPE, UNIQUE_NAME } from '../symbols.js'
import type { DateFieldOptions, EnumLike, FieldContext, SchemaTypes } from '../types.js'
import type {
UndefinedOptional,
DateFieldOptions,
EnumLike,
FieldContext,
SchemaTypes,
} from '../types.js'

/**
* Schema builder exposes methods to construct a Vine schema. You may
Expand Down Expand Up @@ -94,15 +100,15 @@ export class SchemaBuilder extends Macroable {
object<Properties extends Record<string, SchemaTypes>>(properties: Properties) {
return new VineObject<
Properties,
{
UndefinedOptional<{
[K in keyof Properties]: Properties[K][typeof ITYPE]
},
{
}>,
UndefinedOptional<{
[K in keyof Properties]: Properties[K][typeof OTYPE]
},
{
}>,
UndefinedOptional<{
[K in keyof Properties as CamelCase<K & string>]: Properties[K][typeof COTYPE]
}
}>
>(properties)
}

Expand Down
26 changes: 13 additions & 13 deletions src/schema/object/group_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ObjectGroup } from './group.js'
import { CamelCase } from '../camelcase_types.js'
import { GroupConditional } from './conditional.js'
import { OTYPE, COTYPE, ITYPE } from '../../symbols.js'
import type { FieldContext, SchemaTypes } from '../../types.js'
import type { FieldContext, SchemaTypes, UndefinedOptional } from '../../types.js'

/**
* Create an object group. Groups are used to conditionally merge properties
Expand All @@ -32,15 +32,15 @@ group.if = function groupIf<Properties extends Record<string, SchemaTypes>>(
) {
return new GroupConditional<
Properties,
{
UndefinedOptional<{
[K in keyof Properties]: Properties[K][typeof ITYPE]
},
{
}>,
UndefinedOptional<{
[K in keyof Properties]: Properties[K][typeof OTYPE]
},
{
}>,
UndefinedOptional<{
[K in keyof Properties as CamelCase<K & string>]: Properties[K][typeof COTYPE]
}
}>
>(conditon, properties)
}

Expand All @@ -52,14 +52,14 @@ group.else = function groupElse<Properties extends Record<string, SchemaTypes>>(
) {
return new GroupConditional<
Properties,
{
UndefinedOptional<{
[K in keyof Properties]: Properties[K][typeof ITYPE]
},
{
}>,
UndefinedOptional<{
[K in keyof Properties]: Properties[K][typeof OTYPE]
},
{
}>,
UndefinedOptional<{
[K in keyof Properties as CamelCase<K & string>]: Properties[K][typeof COTYPE]
}
}>
>(() => true, properties)
}
18 changes: 18 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,21 @@ export type InferInput<Schema extends { [ITYPE]: any }> = Schema[typeof ITYPE]
export type NumericComparisonOperators = '>' | '<' | '>=' | '<='
export type ArrayComparisonOperators = 'in' | 'notIn'
export type ComparisonOperators = ArrayComparisonOperators | NumericComparisonOperators | '=' | '!='

type PickUndefined<T> = {
[K in keyof T]: undefined extends T[K] ? K : never
}[keyof T]

type PickNotUndefined<T> = {
[K in keyof T]: undefined extends T[K] ? never : K
}[keyof T]

type Id<T> = T extends infer U ? { [K in keyof U]: U[K] } : never

export type UndefinedOptional<T> = Id<
{
[K in PickUndefined<T>]?: T[K]
} & {
[K in PickNotUndefined<T>]: T[K]
}
>
Loading

0 comments on commit 54f5237

Please sign in to comment.