typeschema icon indicating copy to clipboard operation
typeschema copied to clipboard

@typeschema/main doesn't infer the type of zod object correctly

Open Melvynx opened this issue 1 year ago • 4 comments

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>;

CleanShot 2024-08-06 at 22 04 52

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.
Fund with Polar

Melvynx avatar Aug 06 '24 20:08 Melvynx

Same issue and it is kind of blocking development @puristajs

Hope, it gets solved soon

sebastianwessel avatar Aug 30 '24 19:08 sebastianwessel

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

sebastianwessel avatar Aug 30 '24 20:08 sebastianwessel

@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

sebastianwessel avatar Aug 31 '24 08:08 sebastianwessel

+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

meslzy avatar Oct 01 '24 16:10 meslzy

+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.

tomfln avatar Oct 04 '24 23:10 tomfln

solved, thanks @nlfmt for the fix!

decs avatar Oct 13 '24 20:10 decs

thanks !

Melvynx avatar Nov 11 '24 02:11 Melvynx