# typescript-unkown-parser

> Parse unknown objects in Typescript

Latest version **0.1.0** (published 2021-04-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install typescript-unkown-parser
pnpm add typescript-unkown-parser
yarn add typescript-unkown-parser
bun add typescript-unkown-parser
```

## 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.1.0 |
| Published | 2021-04-03 |
| First published | 2021-04-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Sebastian Knudsen |
| Maintainers | sebdak |
| Keywords | Parser, Runtime, Type checker, TypeScript |

## Links

- npm: https://www.npmjs.com/package/typescript-unkown-parser
- Repository: https://github.com/sebdak/typescript-unkown-parser
- Homepage: https://github.com/sebdak/typescript-unkown-parser#readme
- Issues: https://github.com/sebdak/typescript-unkown-parser/issues
- npm.io page: https://npm.io/package/typescript-unkown-parser

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 0.1.0 (latest) — 2021-04-03

## README

# TypeScript Unkown Parser 🃏

TypeScript is a great tool for statically enforcing correct types at compile time, but this can quickly become a false security when dealing with unkown data from the "outside world".

typescript-unknown-parser uses ES6 classes to parse unknown objects, and throws en error if

-   required properties are missing
-   properties (required and non-required) are of wrong type.

## Installation

```bash
npm i typescript-unknown-parser
```

## Usage

1. Make a class with expected typed properties. Extend it by UnknownParser imported from typescript-unknown-parser, and pass the class as a generic type argument.
2. Call `super(data, 'MyClass')` with the unknown data and the name of the class
3. Set all properties with the `protected` functions: `getPrimitive, getIsoDate, getArray, getStringAsEnumKey, getArrayOfEnumKeys`

```typescript
import UnknownParser from 'typescript-unknown-parser';

enum MyEnum {
    ONE,
    TWO,
    THREE,
}

class MyClass extends UnknownParser<MyClass> {
    myString?: string;
    myRequiredString: string;
    myRequiredArray: unknown[];
    myRequiredEnum: keyof typeof MyEnum;
    myRequiredEnumArray: (keyof typeof MyEnum)[];

    constructor(data: any) {
        super(data, 'MyClass'); // Pass the name of the class as the second parameter
        this.myString = this.getPrimitive('myString', 'string');
        this.myRequiredString = this.getPrimitive('myRequiredString', 'string', 'REQUIRED');
        this.myRequiredArray = this.getArray('myRequiredArray', 'REQUIRED'); // yields unknown[]
        this.myRequiredEnum = this.getEnum(MyEnum, 'myRequiredEnum', 'REQUIRED');
        this.myRequiredEnumArray = this.getArrayOfEnum(MyEnum, 'myRequiredEnumArray', 'REQUIRED');
    }
}
```

Then, in your application, parse unknown data by calling the MyClass constructor

```typescript
fetch('url.com')
    .then((res) => res.json())
    .then((data: unknown) => {
        const myObject = new MyClass(data);
        // myObject is now guaranteed to be of type MyClass at runtime
    })
    .catch((e) => {
        console.error(e); // TypeError is thrown if the data does not conform to the expected class interface
    });
```

## Versioning

Keep in mind that the API can change at any time before hitting version `1.0.0`.

## Trade-offs and limitations

This library is not designed with memory efficiency in mind, as it does increase the size of the class-instance becuase of the added functions in the `UnknownParser` class. It does, however, try to developer friendly by presenting a familiar API trough class syntax.
This library might not be suitable for you if

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