Skip to content

Commit

Permalink
Fix type inference on main package
Browse files Browse the repository at this point in the history
  • Loading branch information
decs committed Feb 27, 2024
1 parent 257a369 commit 0e597a5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-queens-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@typeschema/main": patch
---

Fix type inference
17 changes: 13 additions & 4 deletions packages/main/src/selector.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import type {AdapterResolvers} from './adapters';
import type {AdapterResolver} from './resolver';
import type {Kind} from '@sinclair/typebox';
import type {SchemaFrom} from '@typeschema/core';
import type {IfDefined, SchemaFrom} from '@typeschema/core';

type IsTypeboxSchema<TSchema> = [IfDefined<typeof Kind>] extends [never]
? false
: TSchema extends {[Kind]: unknown}
? true
: false;
function isTypeboxSchema(
schema: SchemaFrom<AdapterResolver>,
): schema is SchemaFrom<AdapterResolvers['typebox']> {
return typeof schema === 'object' && Symbol.for('TypeBox.Kind') in schema;
}

type ClassValidatorSchema = new (...args: unknown[]) => object;
type IsClassValidatorSchema<TSchema> = TSchema extends new (
...args: unknown[]
) => object
? true
: false;
function isClassValidatorSchema(
schema: SchemaFrom<AdapterResolver>,
): schema is SchemaFrom<AdapterResolvers['classValidator']> {
Expand All @@ -30,10 +39,10 @@ export type Select<TSchema> =
TSchema extends Function
? TSchema extends {assert: unknown}
? 'arktype'
: TSchema extends ClassValidatorSchema
: IsClassValidatorSchema<TSchema> extends true
? 'classValidator'
: 'function'
: TSchema extends {[Kind]: unknown}
: IsTypeboxSchema<TSchema> extends true
? 'typebox'
: TSchema extends {__isYupSchema__: unknown}
? 'yup'
Expand Down

0 comments on commit 0e597a5

Please sign in to comment.