typeschema
typeschema copied to clipboard
@typeschema/main doesn't infer the type of zod object correctly
Today I try this code :
import { Infer, Schema } from '@typeschema/main';
const ZodSchema = z.object({
title: z.string(),
});
type ZodSchemaType = z.infer<typeof ZodSchema>;
type Output = Infer<typeof ZodSchema>;
But the type of output is any, when I replace @typeschema/main by @typeschema/zod the error is fixed.
The documentation is very small, is this a feature or a bug ?
Upvote & Fund
- We're using Polar.sh so you can upvote and help fund this issue.
- We receive the funding once the issue is completed & confirmed by you.
- Thank you in advance for helping prioritize & fund our backlog.
Same issue and it is kind of blocking development @puristajs
Hope, it gets solved soon
Seems not to be zod specific:
import type { Infer } from "@typeschema/main";
import {object,string} from 'yup'
const yupSchema = object({
optionOne: string().required()
})
type test= Infer<typeof yupSchema>
// test = unknown
@decs
The root cause of this issue is, that the lib is using @sinclair/typebox, which is missing in the package dependencies.
Without this dependency, IsTypeboxSchema returns true, even if it is a zod or yup schema, which results in a wrong Select with typebox instead of zod or yup.
export type Select<TSchema> =
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
TSchema extends Function
? TSchema extends {assert: unknown} ? 'arktype'
: IsEffectSchema<TSchema> extends true ? 'effect'
: IsClassValidatorSchema<TSchema> extends true ? 'classValidator'
: 'function'
: TSchema extends object
? IsTypeboxSchema<TSchema> extends true ? 'typebox'
: IsSuretypeSchema<TSchema> extends true ? 'suretype'
: TSchema extends {__isYupSchema__: unknown} ? 'yup'
: TSchema extends {_def: unknown} ? 'zod'
: TSchema extends {async: unknown} ? 'valibot'
: TSchema extends {refiner: unknown} ? 'superstruct'
: TSchema extends {_flags: unknown} ? 'joi'
: TSchema extends {encode: unknown} ? 'ioTs'
: TSchema extends {reflect: unknown} ? 'runtypes'
: TSchema extends {kind: unknown} ? 'deepkit'
: TSchema extends {addValidator: unknown} ? 'ow'
: TSchema extends {toTerminals: unknown} ? 'valita'
: TSchema extends {bail: unknown} ? 'vine'
: IsJSONSchema<TSchema> extends true ? 'json'
: 'fastestValidator'
: never;
Package suretype is also used, but seems not to be in deps as well.
@Melvynx
The temporary workaround/fix is, to manually add the missing dep.
npm i -D @sinclair/typebox
+1 here, its blocking our progress
import { InferIn } from "@typeschema/main";
import { z } from "zod";
const schema = z.object({
name: z.string(),
});
type SchemaInput = InferIn<schema>;
// ? type SchemaInput = unknown
+1, for some reason im only getting the error when running tsc to lint, in vscode all types are correctly inferred. Installing @sinclair/typebox fixes that, but it's not a desirable workaround for libraries.
solved, thanks @nlfmt for the fix!
thanks !