Introduction

Welcome to type-guarder!

A simple, powerful and flexible runtime type checker for TypeScript.

  • Verify types at runtime
  • Automatically access typed values
  • Support for custom types

Install

Install with your favorite package manager:

pnpm i type-guarder

Import T from type-guarder:

import { T } from "type-guarder";
 
T.string().conforms("Hello World!"); // true

tldr

import { T } from "type-guarder";
 
const checker = T.Object({
  name: T.Object({
    first: T.String(),
    last: T.String(),
  }),
  email: T.Regex(^[^\s@]+@[^\s@]+\.[^\s@]+$/),
  phone: T.Nullable(T.string()),
  address: T.Nullable(
    T.Object({
      street: T.String(),
      apt: T.Nullable(T.String()),
      zip: T.Union(T.String(), T.Integer()),
    })
  ),
});
 
// value will now be a typed object
const value = checker.unwrap({
  name: {
    first: "Elijah",
    last: "Cobb",
  },
  email: "elijah@elijahcobb.com",
	phone: null,
	address: {
		street: "1234 Main St.",
		apt: null,
		zip: 12345
	}
});
 
console.log(value.name.first); // Elijah
 

Check out the functions you can use with type-guarder:

Check out the built in types starting with Boolean.

Maintainer

Elijah Cobb