---
id: "strongly-typed-yup-schema-typescript"
title: "Strongly Typed Yup Schema in TypeScript"
description: "A TypeScript pattern that uses generics and conditional types to
  make Yup schemas strongly typed, with examples that satisfy the compiler."
pubDate: "2022-10-01T00:00:00.000Z"
tags:
  - "code"
  - "yup"
  - "typescript"
  - "generics"
syndicate: false
canonicalURL: "https://justin.poehnelt.com/posts/strongly-typed-yup-schema-typescript/"
relativeURL: "/posts/strongly-typed-yup-schema-typescript/"
markdownURL: "/posts/strongly-typed-yup-schema-typescript.md"
---
This weekend I have been exploring [conditional types in TypeScript](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html) to use with Firestore. These take the basic form of `T extends U ? X : Y` and are used to create a new type based on the type of `T`. I was curious if I could use this to create a strongly typed Yup schema.

The basic idea is to create a generic function that takes a Yup schema and a type. The function will return a Yup schema that is strongly typed to the type. The function will use a conditional type to determine the type of the schema and return the appropriate schema.

```typescript
import * as yup from "yup";

export type ConditionalSchema<T> = T extends string
  ? yup.StringSchema
  : T extends number
    ? yup.NumberSchema
    : T extends boolean
      ? yup.BooleanSchema
      : T extends Record<any, any>
        ? yup.AnyObjectSchema
        : T extends Array<any>
          ? yup.ArraySchema<any, any>
          : yup.AnySchema;

export type Shape<Fields> = {
  [Key in keyof Fields]: ConditionalSchema<Fields[Key]>;
};
```

With those two generic types, we can create a strongly typed Yup schema. These can obviously be extended to include more types and better handle arrays and objects.

Some example usage that keeps the TypeScript compiler happy:

```typescript
interface Foo {
  stringField: string;
  booleanField: boolean;
}

yup.object<Shape<Foo>>({
  stringField: yup.string().default(""),
  booleanField: yup.boolean().default(false),
});
```

And it works!


[Strongly Typed Yup Schema in TypeScript](https://justin.poehnelt.com/posts/strongly-typed-yup-schema-typescript/) © 2022 by [Justin Poehnelt](https://justin.poehnelt.com/) is licensed under CC BY-SA 4.0