Skip to content

fix(typebox): improve type interface #757

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Mar 27, 2025

Conversation

kotarella1110
Copy link
Member

@kotarella1110 kotarella1110 commented Mar 26, 2025

resolve #753 (comment)

Unlike other resolvers like zod, which can take Input, Context, and Output as generic arguments, it is not feasible to achieve a similar interface with typebox.

suggested type:

export function typeboxResolver<Schema extends TObject, Context>(
  schema: Schema | TypeCheck<Schema>,
): Resolver<Static<Schema>, Context, StaticDecode<Schema>>;

rationale:

  • In typebox, the TObject type accepts TProperties as an argument, which differs from the Input (FieldValues) concept in other resolvers.
  • As demonstrated below, attempting to generate a schema type from an Input (FieldValues) would result in a type error:
// TypeError: The type '{ id: number; }' does not satisfy the constraint 'TProperties'. Property 'id' is incompatible with the index signature. Type 'number' is not assignable to type 'TSchema'.
const schema: TObject<{ id: number }> = Type.Object({
  id: Type.Transform(Type.Number())
    .Decode((v) => String(v))
    .Encode((v) => Number.parseInt(v)),
});

// Correct implementation:
const schema: TObject<{ id: TTransform<TNumber, string> }> = Type.Object({
  id: Type.Transform(Type.Number())
    .Decode((v) => String(v))
    .Encode((v) => Number.parseInt(v)),
});

If we attempted to replicate the interface seen in zod, it would require defining the schema type explicitly as a generic parameter, which is not practical or realistic for users:

export function typeboxResolver<
  Schema extends TObject,
  Input extends Static<Schema> = FieldValues,
  Context,
  Output extends StaticDecode<Schema>
>(
  schema: Schema | TypeCheck<Schema>,
): Resolver<Input, Context, Output>;

Given these constraints, the initially proposed typing is the most feasible approach for typebox. It prioritizes usability while maintaining type safety.

@kotarella1110 kotarella1110 force-pushed the fix/improve-typebox-resolver-types branch 2 times, most recently from 50d29eb to 1fb56ab Compare March 26, 2025 16:01
Copy link
Member

@jorisre jorisre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the help and the explanation :)

@jorisre jorisre merged commit d441f34 into fix/743 Mar 27, 2025
3 of 4 checks passed
@jorisre jorisre deleted the fix/improve-typebox-resolver-types branch March 27, 2025 07:41
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants