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

ts-type-check

v2.1.0

Published

Check json value based of Typescript type in string

Downloads

21

Readme

ts-type-check

Check json value based of Typescript type in string, throw express-compatible error with 400 statusCode.

npm Package Version

Supported Data Type

  • string
  • number
  • boolean
  • Date
  • null
  • object (with required / optional fields)
  • Array
  • unions
  • intersections
  • unit types*

*: Unit types are the literal value of primitive types, e.g. true | false | 'str' | 1

Typescript signatures

checkTsType() can be used for one-off type checking.

/**
 * only check for json-compatible types
 *
 * @throws TypeCheckError if failed
 * */
function checkTsType(type: string, data: any, options?: TypeCheckOptions): void

type TypeCheckOptions = {
  casualBoolean?: boolean
}

class TypeCheckError extends TypeError {
  statusCode = 400
  status = 400
  message: string
}

parseTsType() compiles the type string into a TypeChecker.

It is optimized for repeating type checking.

(It is used by checkTsType() internally.)

function parseTsType(type: Type): TypeChecker

interface TypeChecker {
  type: string
  check(data: any, options?: TypeCheckOptions): void
}

Example

import { checkTsType } from 'ts-type-check'

checkTsType('string', 'Alice')
// no error

checkTsType('{ UserId: string }', { UserId: 'Alice' })
// no error
checkTsType('{ UserId: string }', { UserId: 'Alice', Foo: 'bar' }, 'fail')
// throw exception complaining extra field `Foo`

More advanced type using nested object, |, and & are also supported.

import { checkTsType } from 'ts-type-check'

checkTsType(`'y' | 'n'`, 'n')
// no error
checkTsType(`1 | 2`, 1)
// no error
checkTsType(`'y' | 'n'`, 'foo')
// throw exception complaining wrong string value
checkTsType(`1 | 2`, 3)
// throw exception complaining wrong number value

checkTsType(
  `{
    UserId: string,
    Contact: {
      Method: 'telegram'
    } & ({ UserId: string } | { Tel: string }) | {
      Method: 'Email',
      Email: string
    }
  }`,
  {
    UserId: 'Alice',
    Contact: {
      Method: 'telegram',
      UserId: 'alice',
    },
  },
)
// no error
checkTsType(
  `{
    UserId: string,
    Contact: {
      Method: 'telegram'
    } & ({ UserId: string } | { Tel: string }) | {
      Method: 'Email',
      Email: string
    }
  }`,
  {
    UserId: 'Alice',
    Contact: {
      Method: 'Email',
      Email_: '[email protected]',
    },
  },
)
// throw exception complaining missing field 'Email' (if the field is given, then will complain extra field 'Email_')

License

This project is licensed with BSD-2-Clause

This is free, libre, and open-source software. It comes down to four essential freedoms [ref]:

  • The freedom to run the program as you wish, for any purpose
  • The freedom to study how the program works, and change it so it does your computing as you wish
  • The freedom to redistribute copies so you can help others
  • The freedom to distribute copies of your modified versions to others