# typechecked

> Typescript runtime type validation

Latest version **0.0.15** (published 2023-12-09) · ISC license · 0 weekly downloads

## Install

```sh
npm install typechecked
pnpm add typechecked
yarn add typechecked
bun add typechecked
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.15 |
| Published | 2023-12-09 |
| First published | 2017-03-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 22.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Alloys Mila |
| Maintainers | mofax |

## Links

- npm: https://www.npmjs.com/package/typechecked
- Repository: https://github.com/mofax/typechecked
- Homepage: https://github.com/mofax/typechecked#readme
- Issues: https://github.com/mofax/typechecked/issues
- npm.io page: https://npm.io/package/typechecked

## Recent versions

- 0.0.15 (latest) — 2023-12-09
- 0.0.14 — 2023-06-29
- 0.0.13 — 2023-06-29
- 0.0.12 — 2023-02-02
- 0.0.11 — 2023-01-16
- 0.0.10 — 2023-01-16
- 0.0.9 — 2023-01-16
- 0.0.8 — 2023-01-16
- 0.0.7 — 2023-01-15
- 0.0.6 — 2023-01-15
- 0.0.5 — 2023-01-15
- 0.0.4 — 2023-01-15
- 0.0.3 — 2023-01-15
- 0.0.2 — 2017-03-03
- 0.0.1 — 2017-03-03

## README

<p align="center">
Runtime type checking for typescript & javascript.
</p>

## Introduction

typechecked is a runtime type checker for typescript & javascript. It allows you to define types and check if a value matches that type. In typescript, it enables you to narrow the type of a value based on a type check.

## Installation

```bash
npm install typechecked
```

## Concept

Typechecked is based on the idea of a simple function, that accepts an unknown value and returns it if it matches a given type. If the value does not match the type, an error is thrown.


```typescript
function value(value: unknown): KnownType {
  // throw if value does not match type
  return value as KnownType;
}
```

The library provides a set of these functions for all primitive types, arrays, objects, tuples, unions, intersections, and more. Types that are more domain specific can easily be defined by the consumer.

### Exported functions

```typescript
import { isString } from 'typechecked';
import { isNumber } from 'typechecked';
import { isInteger } from 'typechecked';
import { isBigInt } from 'typechecked';
import { isBoolean } from 'typechecked';
import { isNully } from 'typechecked';
import { isNull } from 'typechecked';
import { isUndefined } from 'typechecked';
import { isSymbol } from 'typechecked';
import { isDate } from 'typechecked';
import { isObject } from 'typechecked';
import { isFunction } from 'typechecked';
```

### Pipe functions

Pipe functions are used to combine multiple type checks into one. They are useful when you want to check if a value matches other conditions apart from just the type. Or if you want to transform a value from the original type before returning it.

```typescript
import { tcpipe, tcpipe_t } from 'typechecked';
```

#### tcpipe

Used when piping functions but the value must be of one type.

```typescript
const validator = tcpipe(
  isString,
  (value) => value.toUpperCase(),
);

const value: string = validator('hello'); // value === 'HELLO'
```

#### tcpipe_t

Useful when the value is transformed to a different type.

```typescript
const validator = tcpipe_t(
  isString,
  isHex,
  (value) => parseInt(value, 16),
);

const value: number = validator('FF1233'); // value === 16716339
```

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