New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

typechecked

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typechecked

Typescript runtime type validation

latest
Source
npmnpm
Version
0.0.15
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

Runtime type checking for typescript & javascript.

Introduction

typechecked is a runtime type checker for typescript & javascript. It allows you to define types and check if a value matches that type. In typescript, it enables you to narrow the type of a value based on a type check.

Installation

npm install typechecked

Concept

Typechecked is based on the idea of a simple function, that accepts an unknown value and returns it if it matches a given type. If the value does not match the type, an error is thrown.

function value(value: unknown): KnownType {
  // throw if value does not match type
  return value as KnownType;
}

The library provides a set of these functions for all primitive types, arrays, objects, tuples, unions, intersections, and more. Types that are more domain specific can easily be defined by the consumer.

Exported functions

import { isString } from 'typechecked';
import { isNumber } from 'typechecked';
import { isInteger } from 'typechecked';
import { isBigInt } from 'typechecked';
import { isBoolean } from 'typechecked';
import { isNully } from 'typechecked';
import { isNull } from 'typechecked';
import { isUndefined } from 'typechecked';
import { isSymbol } from 'typechecked';
import { isDate } from 'typechecked';
import { isObject } from 'typechecked';
import { isFunction } from 'typechecked';

Pipe functions

Pipe functions are used to combine multiple type checks into one. They are useful when you want to check if a value matches other conditions apart from just the type. Or if you want to transform a value from the original type before returning it.

import { tcpipe, tcpipe_t } from 'typechecked';

tcpipe

Used when piping functions but the value must be of one type.

const validator = tcpipe(
  isString,
  (value) => value.toUpperCase(),
);

const value: string = validator('hello'); // value === 'HELLO'

tcpipe_t

Useful when the value is transformed to a different type.

const validator = tcpipe_t(
  isString,
  isHex,
  (value) => parseInt(value, 16),
);

const value: number = validator('FF1233'); // value === 16716339

FAQs

Package last updated on 09 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts