# is-constructable

> A predicate for testing if a value is constructable (can be called using the new operator)

Latest version **1.0.0** (published 2022-06-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install is-constructable
pnpm add is-constructable
yarn add is-constructable
bun add is-constructable
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-06-11 |
| First published | 2022-06-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Frédérique Mittelstaedt |
| Maintainers | gfmio |

## Links

- npm: https://www.npmjs.com/package/is-constructable
- Repository: https://github.com/gfmio/is-constructable
- npm.io page: https://npm.io/package/is-constructable

## Recent versions

- 1.0.0 (latest) — 2022-06-11

## README

# is-constructable

`is-constructable` provides a type predicate for identifying if a function is constructable, i.e. whether it can be used with the `new` operator, without actually invoking the constructor.

## Install

```sh
# If you use npm
npm install is-constructable

# If you use yarn
yarn add is-constructable
```

## Usage

```ts
import { isConstructable } from "is-constructable";

isConstructable(function () {}); // true
isConstructable(class A {}); // true
isConstructable(Array); // true
isConstructable(Function); // true
isConstructable(new Function()); // true

isConstructable(undefined); // false
isConstructable(null); // false
isConstructable(1); // false
isConstructable(new Number(1)); // false
isConstructable(Array.prototype); // false
isConstructable(Function.prototype); // false
isConstructable({ method() {} }.method); // false

isConstructable(() => {}); // false
isConstructable(async () => {}); // false
isConstructable(async function f() {}); // false
isConstructable(function* f() {}); // false
isConstructable(async function* f() {}); // false
```

> **Note:** If you transpile to ES5 or below, your transpiler will convert some or all of your arrow functions, async functions, async arrow functions, generator functions and async generator functions to plain old functions, which _are_ constructable in contrast to the original objects.

Under the hoood, the library implements two equivalent checks, one using `Reflect` and one using `Proxy`, which can be imported explicitly if so desired.

```ts
import { testIfConstructableWithProxy, testIfConstructableWithReflect } from "is-constructable";

testIfConstructableWithProxy(function () {}); // true
testIfConstructableWithReflect(function () {}); // true
```

`isConstructable` will first attempt the check using `Proxy` and only if that fails, it will try to using the check using `Reflect`, because the `Proxy` check is about 3.3 times faster than the check using `Reflect`.

# License

[MIT](LICENSE)

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