# @tradle/typeforce

> Another typescript-based, biased type checking solution for Javascript

Latest version **2.2.1** (published 2022-03-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tradle/typeforce
pnpm add @tradle/typeforce
yarn add @tradle/typeforce
bun add @tradle/typeforce
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.1 |
| Published | 2022-03-24 |
| First published | 2022-02-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 146.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Daniel Cousens |
| Maintainers | leichtgewicht, genevayngrib, tenaciousmv, pgmemk |
| Keywords | typeforce, types, typechecking, type, exceptions, force |

## Links

- npm: https://www.npmjs.com/package/@tradle/typeforce
- Repository: https://github.com/tradle/typeforce
- Issues: https://github.com/tradle/typeforce/issues
- npm.io page: https://npm.io/package/@tradle/typeforce

## Dependencies (1)

- [debug](https://npm.io/package/debug.md) ^4.3.3

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 2.2.1 (latest) — 2022-03-24
- 2.2.0 — 2022-03-16
- 2.1.0 — 2022-02-23
- 2.0.1 — 2022-02-23
- 2.0.0 — 2022-02-15

## README

# @tradle/typeforce
[![Version](https://img.shields.io/npm/v/@tradle/typeforce.svg)](https://www.npmjs.org/package/@tradle/typeforce)

> This is a fork of [typeforce](https://github.com/dcousens/typeforce) that is based on typescript and comes with strong types.

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

```javascript
const typeforce = require('@tradle/typeforce')
const { assert } = typeforce

// supported primitives 'Array', 'Boolean', 'Buffer', 'Number', 'Object', 'String'
assert('Array', [])

assert('Number', [])
// TypeError: Expected Number, got Array

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

// enforces object properties 
assert({
  foo: 'Number'
}, {
  foo: 'bar'
})
// TypeError: Expected property "foo" of type Number, got String "bar"

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

// sum types
assert(typeforce.anyOf('String', 'Number'), 2)
assert(typeforce.allOf({ x: typeforce.Number }, { y: typeforce.Number }), {
  x: 1,
  y: 2
})

// value types
assert(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
}

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

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

**Pro**tips:
```javascript
// use precompiled primitives for high performance
assert(typeforce.Array, array)

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

const fastType = typeforce.compile(type)
fastType.assert({
  foo: 1
})
fastType.match({
  foo: 2,
  bar: 'world'
})
// fastType => typeforce.object({
//   foo: typeforce.Number,
//   bar: typeforce.maybe(typeforce.String)
// })

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

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

**Pro**tips (extended types):
```javascript
assert(typeforce.tuple('String', 'Number'), ['foo', 1])
// OK!

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

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

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

function Foo () {
  this.x = 2
}

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

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

**Pro**tips (no throw)
```javascript
const typeforce = require('@tradle/typeforce')
const { match } = typeforce
const value = 'foobar'

if (match(typeforce.Number, value)) {
  // didn't throw!
  console.log(`${value} is a number`) // never happens
} else {
  console.log(`Oops, ${typeforce.error.message}`)
  // prints 'Oops, Expected Number, got String foobar'
}
```

**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

[MIT](./LICENSE)

---
_Source: https://npm.io/package/@tradle/typeforce · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
