Git Product home page Git Product logo

guarantee-type's Introduction

guarantee-type

guarantee the type of a plain object

designing npm-version build coverage outdated-deps

language: English also available in: Spanish

Main goal

When we use Typescript, in some point we want to use types everywere. But in some cases is very dificult to avoid the use of any.

For example, when we get data from LocalStorage, we transfer data in the network or we retieve from the database and ower ORM is no typed enought.

type Person = {
    name: string,
    age?: number,
    active: boolean,
    due: Date
};

function print(person: Person){
    // ...
};

print(JSON.parse(localStorage['person1'])) // 💩 receiving any

// Using https://node-postgres.com/
const res = await client.query('SELECT $1::text as message', ['Hello world!'])
console.log(res.rows[0].messagggge)  // 💩 receiving undefined because the typo

Currently Typescript do not avoid the use of any in the right hand of an assignation. But regardless of that we need a way to validate and set types of received data.

import { guarantee, is, GuaranteedType } from "guarantee-type";

var descriptionPerson = is.object({
    name: is.string,
    age: is.optional.number,
    active: is.boolean,
    due: is.class(Date)    // for Date class only you can write is.Date
});

type Person = GuaranteedType<typeof descriptionPerson>;

function print(person: Person){
    // ...
};

print(guarantee(descriptionPerson, JSON.parse(localStorage['person1']))); // 👍 ok, type guaranteed!

// Using https://node-postgres.com/
const res = await client.query('SELECT $1::text as message', ['Hello world!'])
console.log(guarantee( is.object({message: is.string}), res.rows[0]).messagggge)  // 👍 ok, typo detected!

Deep into

Using is for describe the type is optional. The descriptions can be defined in a plain Javascript object that can be serializable and enceded with JSON. For example descriptionPerson can be writen like this:

var descriptionPerson = {
    object: {
        name: { string: {} },
        age: { optional: { number: {} },
        active: { boolean: {} },
        due: { class: Date }
    }
};

License

MIT

guarantee-type's People

Contributors

emilioplatzer avatar

Watchers

 avatar

guarantee-type's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.