npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

auto-type-guard

v0.0.2

Published

Generate type guard and type assertion functions from Typescript types

Downloads

3

Readme

auto-type-guard

Automatically generate type guard and type assertion functions from Typescript types.

Notable dependencies:

  • The validation is based on json-schemas and uses ts-json-schema-generator to generate the schemas from Typescript types. JS-doc annotations are supported to add additional constraints to the generated schemas.
  • The schema generation step uses ts-morph to find calls to createTypeValidator and createTypeGuard
  • Runtime validation uses ajv

Install and setup

  • npm install -S auto-type-guard
  • Call generate-schemas . somewhere in your build process (e.g. in the postbuild script of your package.json), or manually call the command when generated schemas need to be updated. Read Configuration for additional configuration options.
    • Alternatively, the exported generateSchemas() function can be called manually at runtime, but note that this requires your Typescript sources to be accessible.
  • The json-schema output directory (which defaults to <package-path>/json-schemas) should be added to your .gitignore (but not in your .npmignore, since the schemas have to be packaged with the rest of your code).

Usage

Type assertion function

import { createTypeValidatorSync, TypeValidator } from 'auto-type-guard';

type Point = {
    x: number;
    y: number;
};

// The validator function needs to be declared with an explicit type annotation
// The string literal passed to createTypeValidator needs to be strictly equal to the type argument name
const pointValidator: TypeValidator<Point> = createTypeValidatorSync<Point>('Point');

const unknownData: unknown = { x: 12, y: 4 };
pointValidator(unknownData);

// unknownData type has been narrowed to Point
const { x, y } = unknownData;

Type guard

import { createTypeGuardSync } from 'auto-type-guard';

type Point = {
    x: number;
    y: number;
};

// The string literal passed to createTypeGuard needs to be strictly equal to the type argument name
const pointGuard = createTypeGuardSync<Point>('Point');

const unknownData: unknown = { x: 12, y: 4 };
if (pointGuard(unknownData)) {
    // unknownData type has been narrowed to Point
    const { x, y } = unknownData;
}

Configuration

Read the configuration documentation here

Limitations

  • The type argument passed to createTypeValidator/createTypeGuard needs to be a reference to an existing interface or type alias, it cannot be an inlined type (not even an array of type like Point[]). If you need to validate an array, define a type alias for this type:
    import { createTypeGuard } from './validator';
      
    type Point = { x: number; y: number };
    // This will not work
    const inlinedPointArrayGuard = createTypeGuard<Point[]>('PointArray');
    
    // So we define an alias for the array type and use it as the type argument
    type PointArray = Point[];
    const pointArrayGuard = createTypeGuard<PointArray>('PointArray');
  • Overloaded interfaces are not handled properly, only the first definition of the interface will be accounted for.
  • The string argument of createTypeValidator/createTypeGuard must be a string literal strictly equal to the name of the type argument. Because of this, you cannot generate schemas for 2 different types with the same name in the same codebase, unless if these are not generated at the same time (by using the filter or ignore options).
  • Being json-schema based, this package can only validate json-compatible data, e.g. no functions, classes, buffers.