Another biased type checking solution for Javascript
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
Daniel Cousens f7e25429eb fix standard issues 6 years ago
bench fix standard issues 6 years ago
scripts add Range type and decimal tests 6 years ago
test add Range type and decimal tests 6 years ago
.gitignore .gitignore: rm lib 8 years ago
.travis.yml use LTS and update to 9/10 6 years ago
LICENSE switch to ISC license 7 years ago
README.md README: spacing 7 years ago
errors.js errors: resolve Error BLANK for new Node versions 7 years ago
extra.js add Range type and decimal tests 6 years ago
index.js enforce values.length is defined for tuples 7 years ago
native.js native: deprecate Null in favour of Nil 7 years ago
package.json 1.13.0 6 years ago

README.md

typeforce

build status Version

Another biased type checking solution for Javascript.

Exception messages may change between patch versions, as often the patch will change some behaviour that was unexpected and naturally it results in a different error message.

Examples

var typeforce = require('typeforce')

var element = { prop: 'foo' }
var elementNumber = { prop: 2 }
var array = [element, element, elementNumber]

// supported primitives 'Array', 'Boolean', 'Buffer', 'Number', 'Object', 'String'
typeforce('Array', array)

typeforce('Number', array)
// TypeError: Expected Number, got Array

// array types
typeforce(['Object'], array)
typeforce(typeforce.arrayOf('Object'), array)

// supports recursive type templating
typeforce({ prop: 'Number' }, elementNumber)

// maybe types
typeforce('?Number', 2)
typeforce('?Number', null)
typeforce(typeforce.maybe(typeforce.Number), 2)
typeforce(typeforce.maybe(typeforce.Number), null)

// sum types
typeforce(typeforce.oneOf('String', 'Number'))

// value types
typeforce(typeforce.value(3.14), 3.14)

// custom types
function LongString (value, strict) {
  if (!typeforce.String(value)) return false
  if (value.length !== 32) return false
  return true
}

typeforce(LongString, '00000000000000000000000000000000')
// => OK!

typeforce(LongString, 'not long enough')
// TypeError: Expected LongString, got String 'not long enough'

Protips:

// use precompiled primitives for high performance
typeforce(typeforce.Array, array)

// or just precompile a template
var type = {
  foo: 'Number',
  bar: '?String'
}

var fastType = typeforce.compile(type)
// fastType => typeforce.object({
//   foo: typeforce.Number,
//   bar: typeforce.maybe(typeforce.String)
// })

// use strictness for recursive types to enforce whitelisting properties
typeforce({
  x: 'Number'
}, { x: 1 }, true)
// OK!

typeforce({
  x: 'Number'
}, { x: 1, y: 2 }, true)
// TypeError: Unexpected property 'y' of type Number

Protips (extended types):

typeforce(typeforce.tuple('String', 'Number'), ['foo', 1])
// OK!

typeforce(typeforce.tuple('Number', 'Number'), ['not a number', 1])
// TypeError: Expected property "0" of type Number, got String 'not a number'

typeforce(typeforce.map('Number'), {
  'anyKeyIsOK': 1
})
// OK!

typeforce(typeforce.map('Number', typeforce.HexN(8)), {
  'deadbeef': 1,
  'ffff0000': 2
})
// OK!

function Foo () {
  this.x = 2
}

typeforce(typeforce.quacksLike('Foo'), new Foo())
// OK!

// Note, any Foo will do
typeforce(typeforce.quacksLike('Foo'), new (function Foo() {}))
// OK!

WARNING: Be very wary of using the quacksLike type, as it relies on the Foo.name property. If that property is mangled by a transpiler, such as uglifyjs, you will have a bad time.

LICENSE ISC