1.0.3 • Published 1 year ago

@ts-type-guard/ts-type-guard v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

TS Type Guard

@ts-type-guard/ts-type-guard is a TypeScript type guard library that helps you to convert any data to the expected type. It provides many built-in operators like 'str', 'object', 'num', 'optional', etc, which you can use to build complex data structures. If the conversion fails, an error will be thrown.

Installation

To install the package, simply run the following command:

$ npm install @ts-type-guard/ts-type-guard

Usage

Here is an example of how to use @ts-type-guard/ts-type-guard to convert a data to the expected type:

import { str, num, optional, object } from '@ts-type-guard/ts-type-guard';

// Define the data sturcture
const User = object({
    name: str,
    age: num,
    address: optional(str)
});

/*
If you need to get the type of the data structure, just use `ReturnType`.
For example, the type of the user will be:

type User = {
    name: string;
    age: number;
    address: string | undefined;
}
*/
type User = ReturnType<typeof User>;

// This case will succeed since the data is valid
const user1 = User({
    name: 'John Doe',
    age: 32,
    address: '123 Main St'
});

// This case will fail since the data is invalid, missing the required age property
const user2 = User({
    name: 'John Doe',
})

API Reference

OperatorDescription
optionalMakes the given value optional. If the value is undefined, the conversion will succeed with the result of undefined.
orAllows you to use multiple type guards and returns the first successful conversion result.
strConverts the given unknown value to a string type. If the converting is failed, an error will be thrown.
numConverts the given unknown value to a number type. If the converting is failed, an error will be thrown.
constStrConverts the given unknown value to a string type and ensures that it matches a specific constant value. If the converting or the comparison is failed, an error will be thrown.
boolConverts the given unknown value to a boolean type. If the converting is failed, an error will be thrown.
arrayConverts an unknown value to an array of expected types using the provided inner type guard. If the converting is failed, an error will be thrown.
objectConverts an unknown object to the expected type using the provided inner type guards. If any of the conversions are failed, an error will be thrown.
1.0.3

1 year ago

1.0.2

1 year ago

1.0.0

1 year ago