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

jbsnorro-typesafety

v1.3.1

Published

A typescript enforced type-safety library.

Downloads

12

Readme

TypeSafety

Goal

The goal of this package is to help enforcing runtime type safety in Typescript, aided by compile-time errors as much as possible.

Description

The library foremostly contains an object, typesystem, that can do type checks (think obj is T or obj is Partial<T> or obj extends T) and this library helps enforcing this object's implementation.

Example

Suppose we have the following example type:

Then I'd like to check at runtime whether a certain object is actually of that type, like so:

The method is above returns a boolean whether its argument is of type Example. However, you may also want to assert obj for being of that type, e.g. when it's an argument, like so:

Implementation notes

Since unfortunately at runtime the Typescript annotations are not retrievable, to accomplish the above you have to add some boilerplate code per type for which you want check for typesafety.

For example, the implementation of the typesystem above would look something like:

CheckableTypes represents the list of types (with user-defined keys) which you can type check using a statement like typesystem.assert(<key>, <obj>).

The convention includes types like AllTypesDescriptions and TypeDescriptionsFor, which are designed to enforce you to implement TypeSystem correctly. It also aids Intellisense: most of the time you can ctrl+space your way through the boilerplate. This helps preventing mistikes in the implementation of a type description (which would defeat the purpose of this library). Of course you could always circumvent this using any, but, just don't...

So the idea is you just write type descriptions once, and you can assert type safety for ever! Some basic type descriptions have already been implemented in BaseTypeDescriptions.

Available API

I'd kindly like to inform you of the following methods that are designed to help creating type descriptions:

  • create (for when T extends object; primitives are already implemented)
  • conjunct (i.e. T & U)
  • disjunct (i.e. T | U)
  • array (i.e. T[])
  • nullable (i.e. T | null)
  • possiblyUndefined (i.e. T | undefined)
  • possiblyNullOrUndefined (i.e. T | null | undefined)
  • optionalNullable (i.e. T? | null)

Furthermore, the typesystem has the following methods in its API:

  • isExact(key, obj).
  • extends(key, obj) .
  • partial(key, obj).
  • nonStrictPartial(key, obj) (rarely useful).
  • assertion methods for all variants of the above (asserting as opposed to checking)
  • lambda-returning methods for all of the above e.g. isExactF(key)(obj)

Lastly, the TypeSystem constructor takes a function as argument to log to, in case you want to know what exactly is wrong with any type check. Usually I specify console.error.

Extensions

If for example you don't want to bother checking a particular property on an object, you could always check it using the type description with key any, or alternatively the following also exist: !undefined, !null and any!. In the first two, ! is to be read as not, in the last one as the bang operator.

Alternatively, if you bother a lot, you can of course completely implement your own type descriptions, simply implement ITypeDescriptions, and let TypeSystem do the rest, and even guide you on correctly implementing it.