Skip to content

Latest commit

 

History

History
43 lines (34 loc) · 612 Bytes

strict-typing.md

File metadata and controls

43 lines (34 loc) · 612 Bytes

Strict Typing

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';
});