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

ts-refined

v0.5.1

Published

Refined types for Typescript

Downloads

58

Readme

Summary

Refined types are essentially a type paired with a predicate which narrows its set of legal values.

This implementation is lightweight. It involves no boxing and unboxing of the base type. Instead the refined type is formed as an intersection of the base type and a tag type. The tag type is the type of the predicate.

In order for this to work:

  • the predicate must have a type-level representation, and
  • casting must be avoided.

The first involves a small amount of boilerplate. The second is common sense for programming with types.

The examples in this documentaton use the following predicate types:

class NotBlank implements Refinement<string> {
    "@nominal" : "926e6136-a210-4231-ad08-30d14fa218cf";
    test = (a : string) => !/^\s*$/.test(a);
}

class LowerCase implements Refinement<string> {
    "@nominal" : "d852de1e-e229-4077-a069-26068c804a34";
    test = (a : string) => a === a.toLowerCase();
}

class BasicLatin implements Refinement<string> {
    "@nominal" : "6023125b-f468-4d6c-b76f-8b8372e83b17";
    test = (a : string) => /^\w*$/.test(a);
}

Refinement predicate type

Predicates should implement this type as a named class so they have a unique type-level identifier.

<A> : the base type to refine.

export type Refinement<A> = {
    test : (a : A) => boolean;
};

The Nil predicate type

The tautological (always true) predicate.

export class Nil implements Refinement<any> {
    "@nominal" : "4079fcdd-99ff-4568-81d4-012b49d112dd";
    test = (_ : any) => true;
}

The Tagged predicate type

Like Nil, but typed for inheritance.

export class Tagged<A> implements Refinement<A> {
    test = (_ : A) => true;
}

Refinement type

The shape of a refined type.

  • <A> the base type that is being narrowed.
  • <T> the identifier tag, an intersection of refinement predicate types.
export type Refined<A, T> = {
    "@nominal" : "9b68e4a3-162a-4574-ba15-347474197c4b";
    "@tag" : T & Nil;
} & A;

Type guards

guard()

A type guard that checks that its first argument matches the supplied predicates.

An example type signature:

guard(a : string, NotBlank, LowerCase)
  : a is Refined<string, NotBlank & LowerCase>;
export function guard<A,
    T extends Refinement<A>
    >(
        a : A,
        t : {new() : T}
    ) : a is Refined<A, T>;

export function guard<A,
    T extends Refinement<A>,
    U extends Refinement<A>
    >(
        a : A,
        t : Ctor<T>,
        u : Ctor<U>
    ) : a is Refined<A, T & U>;

export function guard<A,
    T extends Refinement<A>,
    U extends Refinement<A>,
    V extends Refinement<A>
    >(
        a : A,
        t : Ctor<T>,
        u : Ctor<U>,
        v : Ctor<V>
    ) : a is Refined<A, T & U & V>;

export function guard<A>(a : A, ...xs : Array<Ctor<Refinement<A>>>) : a is Refined<A, any> {
    return guardImpl(a, ...xs);
};

guards()

A type guard that checks that its first argument matches the supplied predicates. Intersects the predicate tag of the first argument with the supplied predicates.

An example type signature:

guards(a : Refined<string, NotBlank>, LowerCase, BasicLatin)
  : a is Refined<string, NotBlank, & LowerCase & BasicLatin>;

Use guards instead of guard if you want to pass an already refined value and not lose its existing predicate tag.

export function guards<A,
    R,
    T extends Refinement<A>
    >(
        a : Refined<A, R>,
        t : Ctor<T>
    ) : a is Refined<A, R & T>;

export function guards<A,
    R,
    T extends Refinement<A>,
    U extends Refinement<A>
    >(
        a : Refined<A, R>,
        t : Ctor<T>,
        u : Ctor<U>
    ) : a is Refined<A, R & T & U>;

export function guards<A,
    R,
    T extends Refinement<A>,
    U extends Refinement<A>,
    V extends Refinement<A>
    >(
        a : Refined<A, R>,
        t : Ctor<T>,
        u : Ctor<U>,
        v : Ctor<V>
    ) : a is Refined<A, R & T & U & V>;

export function guards<A, R>(
    a : Refined<A, R>,
    ...xs : Array<Ctor<Refinement<A>>>
) : a is Refined<A, R> {
    return guardImpl(a, ...xs);
};

Lifting values into refined types

lift()

Lift the first argument into a refined type or throw if a predicate fails.

Example of success:

type NBLC = Refined<string, NotBlank & LowerCase>;
const a : NBLC = lift("jabberwock", NotBlank, LowerCase);

Example of failure:

type NBLC = Refined<string, NotBlank & LowerCase>;
const a : NBLC = lift("SHOUT", NotBlank, LowerCase);
// => throws Error: Refinement error: [NotBlank, LowerCase]: SHOUT.
export function lift<A,
    T extends Refinement<A>
    >(
        a : A,
        t : Ctor<T>
    ) : Refined<A, T>;

export function lift<A,
    T extends Refinement<A>,
    U extends Refinement<A>
    >(
        a : A,
        t : Ctor<T>,
        u : Ctor<U>
    ) : Refined<A, T & U>;

export function lift<A,
    T extends Refinement<A>,
    U extends Refinement<A>,
    V extends Refinement<A>
    >(
        a : A,
        t : Ctor<T>,
        u : Ctor<U>,
        v : Ctor<V>
    ) : Refined<A, T & U & V>;

export function lift<A>(
    a : A,
    ...xs : Array<Ctor<Refinement<A>>>
) : Refined<A, any> {
    if (guardImpl(a, ...xs)) {
        return a;
    }
    throw new Error(`Refinement error: ${xs}: ${a}.`);
};

lifts()

Lift the first argument into a refined type or throw if a predicate fails. Intersects the predicate tag of the first argument with the supplied predicates.

Example of success:

type NB = Refined<string, NotBlank>;
type NBLC = Refined<string, NotBlank & LowerCase>;
const a : NB = lift("jabberwock", NotBlank);
const b : NBLC = lifts(a, LowerCase);

Example of failure:

type NB = Refined<string, NotBlank>;
type NBLC = Refined<string, NotBlank & LowerCase>;
const a : NB = lift("SHOUT", NotBlank);
const b : NBLC = lifts(a, LowerCase);
// => throws Error: Refinement error: [LowerCase]: SHOUT.
export function lifts<A,
    R,
    T extends Refinement<A>
    >(
        a : Refined<A, R>,
        t : Ctor<T>
    ) : Refined<A, R & T>;

export function lifts<A,
    R,
    T extends Refinement<A>,
    U extends Refinement<A>
    >(
        a : Refined<A, R>,
        t : Ctor<T>,
        u : Ctor<U>
    ) : Refined<A, R & T & U>;

export function lifts<A,
    R,
    T extends Refinement<A>,
    U extends Refinement<A>,
    V extends Refinement<A>
    >(
        a : Refined<A, R>,
        t : Ctor<T>,
        u : Ctor<U>,
        v : Ctor<V>
    ) : Refined<A, R & T & U & V>;

export function lifts<A, R extends Refinement<A>>(
    a : Refined<A, R>,
    ...xs : Array<Ctor<Refinement<A>>>
) : Refined<A, R> {
    if (guardImpl(a, ...xs)) {
        return a;
    }
    throw new Error(`Refinement error: ${xs}: ${a}.`);
};

liftUnsafe()

Lift the first argument into the refined type tagged with the second type parameter.

This is just a cast, but its name highlights that you better know what you are doing.

Example:

type NB = Refined<string, NotBlank>;
const a : NB = liftUnsafe<string, NotBlank>("abc");
const b : NB = liftUnsafe<string, NotBlank>(" ");

Both 'a' and 'b' will succeed, with the type of b now a potentially dangerous misrepresentation.

export const liftUnsafe =
    <A, R>(a : A) : Refined<A, R> => a as Refined<A, R>;