Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

common-expectations

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

common-expectations

Typescript runtime assertions

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Common Expectations

A small collection of common typescript functions for asserting and guarding types.

Installation

# using npm
npm install common-expectations
# or using yarn
yarn add common-expectations

Usages

exists<T>(value: T, message: string): void

A utility for asserting existence of a value when the type may be undefined or null.

import { exists } from "common-expectations";

// where use params returns a partial type where `id` maybe undefined.
const { id } = useParams<{ id: string }>();
exists(value, 'Expected id to be defined.')

This is intended only to be used in those rare cases where you know based on context that a value exists but the type system is unable to infer the fact. A good example of this is when using params in React Router.

isUnreachable(value: never, message?: string): void

A utility for helping check type exhaustiveness. It will raise typescript type error when a values type is anything other than never, and will throw an error at runtime if a value is encountered.

import { isUnreachable } from "common-expectations";

type AnimalType = 'cat' | 'dog' | 'monkey';

function doSomthing(type: AnimalType) {
  switch (type) {
    case "cat":
    case "dog":
    case "monkey":
      break;
    default:
      isUnreachable(type, `Encountered unexpected animal type: ${type}`);
  }
}

Keywords

FAQs

Package last updated on 28 Jul 2022

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc