# @frankhoodbs/cast-prop-to-type

> Simple utility to convert string props to correct type

Latest version **5.0.0** (published 2026-03-04) · ISC license · 0 weekly downloads

## Install

```sh
npm install @frankhoodbs/cast-prop-to-type
pnpm add @frankhoodbs/cast-prop-to-type
yarn add @frankhoodbs/cast-prop-to-type
bun add @frankhoodbs/cast-prop-to-type
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2026-03-04 |
| First published | 2023-04-21 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Frankhood Business Solutions s.r.l. |
| Maintainers | giando110188, frankhood-info, skill83, m.brandonisio |

## Links

- npm: https://www.npmjs.com/package/@frankhoodbs/cast-prop-to-type
- npm.io page: https://npm.io/package/@frankhoodbs/cast-prop-to-type

## Recent versions

- 5.0.0 (latest) — 2026-03-04
- 4.0.2 — 2025-11-26
- 4.0.1 — 2025-11-12
- 4.0.0 — 2025-10-24
- 3.0.4 — 2025-07-17
- 3.0.3 — 2025-06-04
- 3.0.2 — 2025-05-27
- 3.0.1 — 2025-05-15
- 3.0.0 — 2025-04-24
- 2.0.3 — 2024-11-05
- 2.0.2 — 2024-10-01
- 2.0.1 — 2024-09-30
- 1.3.12 — 2024-09-25
- 1.3.11 — 2024-09-17
- 1.3.10 — 2024-07-15
- … 43 more at https://npm.io/package/@frankhoodbs/cast-prop-to-type/versions

## README

# Cast Prop to Type

This utility provides a straightforward mechanism to cast various types of JavaScript properties to desired data types, such as `number`, `array`, `boolean`, and `object`. In the event of a casting error or invalid input, a custom `ValidationError` is thrown to provide a meaningful error message.

![Version](https://img.shields.io/npm/v/@frankhoodbs/cast-prop-to-type)
![License](https://img.shields.io/npm/l/@frankhoodbs/cast-prop-to-type)

## Usage

```javascript
import { castPropToType } from "@frankhoodbs/cast-prop-to-type";

const numValue = castPropToType("42", "number");  // Returns 42 as a number
```

## API

### `castPropToType(value, type)`

Cast the given `value` to the specified `type`.

#### Parameters:

- **value** (`any`): The value to be casted.
- **type** (`"number" | "array" | "boolean" | "object"`): The type to which the value should be casted.

#### Returns:

The casted value. Throws `ValidationError` if casting is not possible.

### ValidationError

Custom error that is thrown when there's a validation error during the casting process. It extends the native JavaScript `Error` class.

## Examples

```javascript
// Casting a string to a number
const num = castPropToType("42", "number"); // 42

// Casting a string to an array
const arr = castPropToType('["a", "b", "c"]', "array"); // ["a", "b", "c"]

// Casting a string to a boolean
const boolTrue = castPropToType("true", "boolean");  // true

// Casting a string to an object
const obj = castPropToType('{"key": "value"}', "object");  // { key: "value" }
```

## Error Handling

If the utility encounters an invalid value for the desired casting type or if the type itself is invalid, a `ValidationError` will be thrown:

```javascript
try {
  const num = castPropToType("foo", "number");
} catch (error) {
  if (error instanceof ValidationError) {
    console.error(error.message);
  }
}
```

---
_Source: https://npm.io/package/@frankhoodbs/cast-prop-to-type · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
