freeze-types

0.1.1 • Public • Published

FreezeTypes

Take any object value and lock down the types of its writable properties.

const obj = freezeTypes({
    name: 'John Smith'
})
 
obj.name = 'Jimmy'
obj.name = 4 // throws
TypeError: Cannot set type-frozen property name from type String to type Number.

    .../.../your-js-file.js:75:54
    ...

ES5/6 Class Support

class User {
    constructor(name, age) {
        this.name = name
        this.age = age
        freezeTypes(this)
    }
}
 
const user = new User
user.name = 35  // throws
user.age = 'Jimmy'  // throws

Extend and re-freeze.

Type-frozen objects are not meant to behave as if they were statically typed. Instead, they are freely extendable, and after any extension, an object can be re-frozen, as follows:

const obj = {name: 'John Smith'}
freezeTypes(obj)
 
obj.age = 24  // Extend the object with an age property.
freezeTypes(obj)  // Re-freeze types to capture the extension.
 
obj.age = false  // throws

However, once frozen, an object cannot be un-frozen (except by painful, fussy effort). Moreover, a frozen property cannot be deleted (frozen properties are non-configurable).

Readme

Keywords

none

Package Sidebar

Install

npm i freeze-types

Weekly Downloads

0

Version

0.1.1

License

MIT

Last publish

Collaborators

  • jeffmcmahan