# ts-guard-clause

> > yarn add ts-guard-clause

Latest version **0.0.7** (published 2021-09-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-guard-clause
pnpm add ts-guard-clause
yarn add ts-guard-clause
bun add ts-guard-clause
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.7 |
| Published | 2021-09-28 |
| First published | 2021-09-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | donnyroufs |
| Maintainers | iamchets |

## Links

- npm: https://www.npmjs.com/package/ts-guard-clause
- npm.io page: https://npm.io/package/ts-guard-clause

## Recent versions

- 0.0.7 (latest) — 2021-09-28
- 0.0.6 — 2021-09-26
- 0.0.5 — 2021-09-26
- 0.0.4 — 2021-09-26
- 0.0.3 — 2021-09-26
- 0.0.2 — 2021-09-26
- 0.0.1 — 2021-09-26

## README

# ts-guard-clause

> yarn add ts-guard-clause

**Before:**

```ts
if (body.name !== undefined) {
  throw new Error('Name is undefined')
}

console.log(name)
```

**After:**

```ts
const name = Guard.against.undefined(body.name)

console.log(name)
```

**Shorter version:**

```ts
const name = Against.undefined(body.name)

console.log(name)
```

**Add a custom message/error**

```ts
Guard.against.undefined(body.name, 'Custom Error')
// OR
Guard.against.undefined(body.name, new MyCustomError('Some Error'))
```

---

## Current clauses

- null
- undefined
- nullish (null and undefined)
- nullishOrEmpty (null, undfined, not an empty array)
- expression (custom condition)

## Add your own guard clause(s)

First you will need to extend the **IGuardClause** interface:

```ts
// @types/ts-guard-clause/index.d.ts
import 'ts-guard-clause'

declare module 'ts-guard-clause' {
  interface IGuardClause {
    hello<T>(prop: T): T
  }
}
```

If everything went well then the AddExtension method will give you intellisense for the `hello` clause.

```ts
import { Guard, GuardFactory } from 'ts-guard-clause'

GuardFactory.AddExtension('hello', (prop) => prop === 1)

Guard.against.Hello('hello') // Throws
```

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