npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typescript-unkown-parser

v0.1.0

Published

Parse unknown objects in Typescript

Downloads

3

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

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
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

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