# boxed_value

> A library for boxed values in typescript.

Latest version **1.0.0** (published 2016-11-13) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2016-11-13 |
| First published | 2016-11-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Rick Carlino |
| Maintainers | rickcarlino |
| Keywords | typescript |

## Links

- npm: https://www.npmjs.com/package/boxed_value
- Repository: https://github.com/rickcarlino/boxed_value
- Issues: https://github.com/rickcarlino/boxed_value/issues
- npm.io page: https://npm.io/package/boxed_value

## Dependencies (1)

- [typescript](https://npm.io/package/typescript.md) ^2.0.9

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2016-11-13

## README

# Boxed Value

An NPM module to bring certainty to uncertain types.

## Why?

You use typescript because it provides compile time type safety.

Sometimes you find yourself needing type safety *after* compile time.
Usually this happens when you are parsing serialized data that entered the application after compile time (Parsing API responses, processing unstructured JSON data, etc..)  

If you've used lodash functions like `_.isArray()` or `_.isBoolean()`, this is sort of the same thing, but hyper focused on TypeScript.

Here's an example:

```typescript

// You have no idea what value this is:
let uncertainty = JSON.parse(iDontKnowYouTellMe);
// At this point, typescript will mark the variable as type an "any" type.
//  We don't want that. Let's box it.
import { box } from "boxed_value";

let boxed = box(uncertainty);
switch (boxed.kind) {
    case "string":
        // We can safely do string operations here, because it is guaranteed to be
        // a string type.
        let guaranteedToBeAString = boxed.value;
        // "Unbox" a variable by calling `.value`.
        guaranteedToBeAString.split("");
    case "boolean":
        console.log("Typescript can type check this as `boolean` safely now!");
    default:
        // Default branch is a great place to throw out junk data.
        throw new Error("¯\_(ツ)_/¯");
}
sw
```

## Other Problems Solved

Boxed values can provide mutability to values that would otherwise be immutable.

```typescript

// BEFORE:
var x = 2;
var y = x;
y = 3;
x === 3; // False, because primitive types are passed by value in JS.

// AFTER:
var x = box(2);
var y = x;
y.value = 3;
x.value === 3; // True, because objects are pass by refernce in JS.

```

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