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

reflect-types

v1.0.1

Published

Generate and maintain type information at runtime

Downloads

11

Readme

This is a small package that provides a simple interface for elegantly defining types in JavaScript at runtime. These types can in turn easily be embedded in any other library or application, such as a SQL schema builder or a JavaScript object validator.

:warning: This library does not contain anything else than a clean method for creating types. It is up to the implementation to provide fancy things such as type inference, constraint resolution, and so on.

const { TypeSet } = require('reflect-types');

Using Babel or TypeScript:

import TypeSet from "reflect-types"

Defining Types

const ts = new TypeSet();
const Types = ts.types;

Simple Types

Types are created simply by calling a property of the Types object:

const numType = Types.Number();

Types Literals

Types can hold literals, similar the type literals we are used to in TypeScript or to constexpr template parameters in C++.

const stringLit = Types.String("only match me!");

Generic Types

In the same way, types can be generic, i.e. accept a number of type arguments:

const promiseType = Types.Promise(Types.Number(), Types.FSError());

Of course, the two can be combined:

const myArrType = Types.FixedArray(Types.String("foo"), 4);

Type Aliases

You can also define type aliases just as easily:

Types.Foo = Types.Bar;
Types.Bar = Types.Number;
const fooType = Types.Foo();
const barType = fooType.expand();
const numType = barType.expand();

// or for short:
const numType = Types.Foo().resolve();

Generic Type Aliases

You can use JavaScript's default methods of abstraction to avoid re-writing the same types again and again:

function CallbackType(resultType: Type, errorType = Types.Error()) {
  return Types.Function({
    returnType: Types.Any(),
    paramTypes: [ErrorType, ResultType];
  });
}

Hey, wait! What if we could assign a type a certain abstraction? This is exactly what generic type aliases are!

Types.Callback = (resultType: Type, errorType = Types.Error()) => Types.Function({
  returnType: Types.Any(),
  paramTypes: [ErrorType, ResultType];
});

Types.Callback(Types.Buffer(), Types.FSError());

API

const t = Types.Number();

assert.strictEqual(t.name, 'MyType');
assert.empty(t.path);
assert.empty(t.args);

Types.isType(t); // true
Types.isType('bla'); // false;

Type

A type is an object that contains the following properties:

  • owner the owning type set that is associated with this type
  • name the name of the type, which must start with an uppercase letter
  • path a path to the namespace in which the type is defined
  • args arguments passed in when the type was created, if any

In addition, the following methods must be present:

Type.expand()

Expands one type alias of the current type and that may be present in the current owner. Either returns the type itself if no aliases were found or returns a new type with substituted arguments.

Type.resolve()

Resolves all type aliases of the current type and that may be present in the current owner. Either returns the type itself if no aliases were found or returns a new type with substitued arguments.

new TypeSet()

Creates a new type set, which is the entry point for creating types and keeps track of all aliases.

TypeSet.types

A special object, from this moment referred to as Types, that can construct any type. The type will automatically belong to the given type set.

Types.NamespaceName

Creates a new Types object of which newly created types will have a path equal to the sum of all NamespaceNames that were used to create the new Types object.

Types.TypeName = TypeGen

Creates a new type alias under the given TypeName. The object TypeGen should be callable and have a length property that correctly returns the arity of the type.

Types.TypeName(...arguments)

Creates a new type with the name set to TypeName and the given arguments. If this Types object is a namespace, path will contain the full path to the namespace. Otherwise, it will be an empty array.

isType(value)

Checks whether the given value satisfies the constraints described in the previous section.

License

Copyright 2017 Sam Vervaeck

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.