# typed-error

> A class designed to enable easily extending the built-in javascript Error, allowing typed errors.

Latest version **3.2.3** (published 2026-03-12) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install typed-error
pnpm add typed-error
yarn add typed-error
bun add typed-error
```

## Health

**Score 60/100 (C)** — status: stable.

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

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 3.2.3 |
| Published | 2026-03-12 |
| First published | 2014-08-12 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 0 |
| Unpacked size | 35.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 5 |
| Author | Pagan Gazzard |
| Maintainers | page, balena.io |

## Links

- npm: https://www.npmjs.com/package/typed-error
- Repository: https://github.com/balena-io-modules/typed-error
- Issues: https://github.com/balena-io-modules/typed-error/issues
- npm.io page: https://npm.io/package/typed-error

## Recent versions

- 3.2.3 (latest) — 2026-03-12
- 3.2.3-build-add-npm-oidc-permissions-8cc1f47dac7fc4c8d5f04a97b7b929e12ac1ee51-1 (build-add-npm-oidc-permissions) — 2026-03-12
- 3.2.2 — 2023-04-20
- 3.2.2-build-flowzonify-8b3f4191d9ff1f27ee2c2d56cfc23cdc5d823fdc-1 — 2023-04-20
- 3.2.2-build-flowzonify-3cea469cefb40a91f064cc5d0e276f8edd194ec3-1 — 2023-04-18
- 3.2.2-dfunckt-patch-1-eb2bc57e4a168080b3dcb6c7325abec18e76c2ae — 2021-11-19
- 3.2.2-dfunckt-patch-1-e0f2737daa9e15f16d9dd782960a86339bbb4a08 — 2021-07-01
- 3.2.1 — 2020-08-05
- 3.2.1-update-deps-2a76155592b8a72796f0f6bb94920253d4dd8656 — 2020-08-05
- 3.2.1-update-deps-dc41949015399154efb4529183daa0dfc9269b52 — 2020-08-04
- 3.2.0 — 2019-11-20
- 3.2.0-node12-22f293afc187e67b88dab280951eb6e2bb504b24 — 2019-11-20
- 4.0.0-node12-dfce0e7655d7f6b1120ac3267d23cc2f31d185d1 — 2019-11-20
- 4.0.0-node12-9667b15bf4a848189f18a5afbf0a68c6b6176620 — 2019-11-19
- 3.1.0 — 2019-04-01
- … 18 more at https://npm.io/package/typed-error/versions

## README

This module allows you to easily extend the built-in Error type for typed error checking

For typescript:
```typescript
import { TypedError } from 'typed-error'

class MyError extends TypedError {}

try {
	throw new MyError()
} catch(e) {
	console.log(e instanceof MyError) // true
	console.log(e.name) // 'MyError'
	console.log(e.constructor.name) // 'MyError'
	console.log(e.stack) // <stack trace>

	if(e instanceof MyError) {
		console.log('Do custom handling')
	} else {
		console.log('Another type of error')
	}

	// Or
	switch(e.name) {
		case 'MyError':
			console.log('Do custom handling')
		break;
		default:
			console.log('Another type of error')
	}

	// Or
	switch(e.constructor.name) {
		case 'MyError':
			console.log('Do custom handling')
		break;
		default:
			console.log('Another type of error')
	}
}
```

And with bluebird:
```typescript
import { TypedError } from 'typed-error'
import * as Promise from 'bluebird'

class MyError extends TypedError {}

Promise.try(() => {
	throw new MyError()
})
.catch(MyError, (e) => {
	console.log('Do custom handling')
})
.catch(() => {
	console.log('Another type of error')
})

// Or
const MyErrorName = (e: Error) => e.name === 'MyError'
Promise.try(() => {
	throw new MyError()
})
.catch(MyErrorName, (e) => {
	console.log('Do custom handling')
})
.catch(() => {
	console.log('Another type of error')
})

// Or
const MyErrorConstructorName = (e: Error) => e.constructor.name === 'MyError'
Promise.try(() => {
	throw new MyError()
})
.catch(MyErrorConstructorName, (e) => {
	console.log('Do custom handling')
})
.catch(() => {
	console.log('Another type of error')
})
```

For coffeescript:
```coffeescript
{ TypedError } = require 'typed-error'

class MyError extends TypedError

try
	throw new MyError()
catch e
	console.log(e instanceof MyError) # true
	console.log(e.name) # 'MyError'
	console.log(e.constructor.name) # 'MyError'
	console.log(e.stack) # <stack trace>

	if e instanceof MyError
		console.log('Do custom handling')
	else
		console.log('Another type of error')

	# Or
	switch e.name
		when 'MyError'
			console.log('Do custom handling')
		else
			console.log('Another type of error')

	# Or
	switch e.constructor.name
		when 'MyError'
			console.log('Do custom handling')
		else
			console.log('Another type of error')
```

And with bluebird:
```coffeescript
Promise = require 'bluebird'
{ TypedError } = require 'typed-error'

class MyError extends TypedError

Promise.try ->
	throw new MyError()
.catch MyError, (e) ->
	console.log('Do custom handling')
.catch ->
	console.log('Another type of error')

# Or
MyErrorName = (e) -> e.name is 'MyError'
Promise.try ->
	throw new MyError()
.catch MyErrorName, (e) ->
	console.log('Do custom handling')
.catch ->
	console.log('Another type of error')

# Or
MyErrorConstructorName = (e) -> e.constructor.name is 'MyError'
Promise.try ->
	throw new MyError()
.catch MyErrorConstructorName, (e) ->
	console.log('Do custom handling')
.catch ->
	console.log('Another type of error')
```

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