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

@apparts/types

v9.9.0

Published

A Typechecking Library

Downloads

73

Readme

#+TITLE: @apparts/types #+DATE: [2019-08-26 Mon] #+AUTHOR: Philipp Uhl

This package provides functions for checking correctness of values against certain types.

  • Types

A type is defined by an object. The object must either contain a key =type= that is an atomic type or be of the form of =object=, =array=, =oneOf=, =value= as described under "Compound types".

All types definitions can be produced by the use of functions from =schema=:

#+BEGIN_SRC js import { schema } from "@apparts/types"; #+END_SRC

It is recommended, to use these functions to build type definitions, as they can also be used to infer TypeScript types.

** Atomic types:

  • ~/~ (catch all)
  • ~base64~
  • ~boolean~
  • ~email~
  • ~float~
  • ~hex~
  • ~int~
  • ~null~
  • ~phoneISD~ (phone number with country code prefix)
  • ~string~
  • ~uuidv4~

The type definition for each of the atomic types looks like: ={ type: }=.

Build a type definition for an atomic type with these functions: #+BEGIN_SRC js import { schema } from "@apparts/types"; const intSchema = schema.int(); const floatSchema = schema.float(); const booleanSchema = schema.boolean(); const stringSchema = schema.string(); const hexSchema = schema.hex(); const uuidv4Schema = schema.uuidv4(); const base64Schema = schema.base64(); const emailSchema = schema.email(); const phoneISDSchema = schema.phoneISD(); const nillSchema = schema.nill(); // { type: "null" } const anySchema = schema.any(); // { type: "/" } #+END_SRC

Certain types only differ in semantics from other more basic types. They are created like this:

#+BEGIN_SRC js // Creating ids depends on how your Ids look like, either a string or an int const idIntSchema = schema.int().semantic("id"); // { type: "int", semantic: "id" } const idStrSchema = schema.string().semantic("id"); // { type: "string", semantic: "id" }

const passwordSchema = schema.string().semantic("password"); // { type: "string", semantic: "password" } const timeSchema = schema.int().semantic("date"); // { type: "int", semantic: "date" } const timeSchema = schema.int().semantic("time"); // { type: "int", semantic: "time" } const timeSchema = schema.int().semantic("daytime"); // { type: "int", semantic: "daytime" } #+END_SRC

** Compound types

Compound objects make it possible to check complex JSON values for validity. Any sub-type can be either an atomic type or a compound type.

  • =object= :: Matches if the value is an object and all the values of the object have the types as specified by =values=, or if the specific keys of the object are known, as specified by the key in =keys=.

    • With known keys: #+BEGIN_SRC js // build type schema import { schema } from "@apparts/types"; const objSchema = schema.obj({ : , : , // ... });

      const typeDefinition = { type: "object", keys: { : { type: [, optional: true]}, ... } } // = objSchema.getType(); #+END_SRC

    • With unknown keys: #+BEGIN_SRC js // build type schema import { schema } from "@apparts/types"; const objSchema = schema.objValues(); const typeDefinition = { type: "object", values: } // = objSchema.getType(); #+END_SRC

  • =array= :: Matches if the value is an array and all items of the array match the type, as specified by =items=. #+BEGIN_SRC js // build type schema import { schema } from "@apparts/types"; const arraySchema = schema.array(); const typeDefinition = { type: "array", items: } // = arraySchema.getType(); #+END_SRC

  • =oneOf= :: Matches if at least one of the alternatives matches #+BEGIN_SRC js // build type schema import { schema } from "@apparts/types"; const oneOfSchema = schema.oneOf([ , , // ... ]);

    const typeDefinition = { type: "oneOf", alternatives: [ , ... ] } // = oneOfSchema.getType(); #+END_SRC

  • =value= :: Matches the exact content #+BEGIN_SRC js // build type schema import { schema } from "@apparts/types"; const valueSchema = schema.value();

    const typeDefinition = { value: } // = valueSchema.getType(); #+END_SRC

** Using Schemas

One can build types by hand by constructing the type definition object. This is not recommended though, as it is easy to mess up and no TypeScript types can be inferred. Instead, @apparts/types provides functions to build a type definition:

#+BEGIN_SRC js // the functions then are available through schema. import { schema } from "@apparts/types"; // or directly from the package import { int, float, boolean, string, hex, uuidv4, base64, email, nill, any, array, obj, oneOf, value, InferType } from "@apparts/types"; #+END_SRC

Using a schema, one can get the type definition with the =getType= function:

#+BEGIN_SRC js const userSchema = schema.obj({ firstName: string(), lastName: string(), gender: string().optional(), }); userSchema.getType(); // returns the type definition #+END_SRC

Also, one can get a TypeScript type:

#+BEGIN_SRC ts type User = InferType;

// The resulting type looks like this: type User = { firstName: string; lastName: string; gender?: string; }; #+END_SRC