tsafe
GitHub
  • 👋What is tsafe?
  • ⚙️How to import
  • assert
  • Equals
  • id
  • is
  • objectKeys
  • exclude
  • isAmong
  • symToStr
  • ReturnType
  • Parameters
  • Param0
  • typeGuard
  • capitalize/uncapitalize
  • MethodNames
  • isPromiseLike
  • flip
  • objectEntries
  • objectFromEntries
  • UnionToIntersection
  • 🚧withDefaults
  • 📉UnpackPromise
Powered by GitBook
On this page
  • Type level testing
  • Making sure that a zod schema validates a given type
  • Making sure all cases of a switch are dealt with

Was this helpful?

Edit on GitHub

Equals

Let you test if two types are the same

PreviousassertNextid

Last updated 5 months ago

Was this helpful?

Type level testing

Making sure that a zod schema validates a given type


import { z } from "zod";
import { assert, type Equals } from "tsafe/assert";
import { id } from "tsafe/id";

type Xyz= // ...

const zXyz = (()=>{

   type TargetType = Xyz;
   
   const zTargetType = z.;
   
   type InferredType = z.infer<typeof zTargetType>;
   
   assert<Equals<TargetType, InferredType>>;
   
   return id<z.ZodType<TargetType>>(zTargetType);   

})();

Making sure all cases of a switch are dealt with

A less trivial example: and it's .

If you are writing tests for your type, you definitely want to checkout

The code
corresponding test file
//@ts-expect-error
Playground
Playground
Playground