reflect-types
v1.0.1
Published
Generate and maintain type information at runtime
Downloads
11
Maintainers
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 NamespaceName
s 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.