We can generate classes that enforce run-time type checks and prevent object extensions
const MyClass = M({
str: String,
arr: [Number],
obj: {
bool: Boolean
}
});
const myInstance = new MyClass();
Now myInstance
is a normal JavaScript object
assert.deepEqual(myInstance, {
str: '',
arr: [0],
obj: {
bool: false
}
});
that prevents incorrect type assignments
assert.throws(function() {
myInstance.obj.bool = 'will throw';
});
and object extensions
assert.throws(function() {
myInstance.newProperty = 'will throw';
});