Skip to content

How to defining Valibot schemas that agree with a TypeScript type? #377

Answered by fabian-hiller
hungify asked this question in Q&A
Discussion options

You must be logged in to vote

You can use BaseSchema:

import * as v from 'valibot';

type Player = {
  name: string;
  age?: number | undefined;
  active: boolean | null;
};

export const Player = v.object({
  name: v.string(),
  age: v.optional(v.number()),
  active: v.nullable(v.boolean()),
}) satisfies v.BaseSchema<Player>;

Replies: 1 comment 9 replies

Comment options

You must be logged in to vote
9 replies
@fabian-hiller
Comment options

@fabian-hiller
Comment options

@hungify2022
Comment options

@hungify2022
Comment options

@fabian-hiller
Comment options

Answer selected by fabian-hiller
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants