A set of classes and utilities for JavaScript type inspection and validation.
- Allows for run-time validation and checking of dynamic types as well as integrated type-guarding.
- Super easy to use, basically only requires a copy paste.
- Works with literals!
interface MyType
{
a: object;
b: string;
c: number;
d: boolean;
e: {
f: string;
g: boolean;
h: [
number,
boolean,
string
];
};
}
The following can be done with pure JavaScript and still work.
const myTypeValidator = new TypeValidator<MyType>(
{
a: Object,
b: String,
c: Number,
d: Boolean,
e: {
f: String,
g: Boolean,
h: [
Number,
Boolean,
String
]
}
});
const myItem = {
a: {},
b: 'hello',
c: 1,
d: true,
e: {
f: 'whatever',
g: false,
h: [
0,
true,
'2'
]
},
i: 'noise'
};
// no compile-time type errors!
if (MyTypeValidator.isSubsetOf(myItem)) {
console.log(myItem.e.h.length); // 3
console.log(myItem.b); // "hello"
} else {
throw new TypeError('Invalid type!');
}