Join our webinar on Wednesday, June 26, at 1pm EDTHow Chia Mitigates Risk in the Crypto Industry.Register
Socket
Socket
Sign inDemoInstall

ts-exhaustive-check

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ts-exhaustive-check

Exhaustive check for typescript type never


Version published
Weekly downloads
415
decreased by-0.95%
Maintainers
1
Created
Weekly downloads
 

Changelog

Source

[1.0.0]

  • First release.

Readme

Source

ts-exhaustive-check

npm version MIT license

Exhaustive check for typescript type never.

Description

This package has a single function called exhaustiveCheck that just checks if a typescript type is of type never.

It will save you one line of code in the default case of switch statements if you have strictNullChecks enabled.

For more information see discriminated-unions and typescript issue 6155.

Usage

yarn add ts-exhaustive-check
import { exhaustiveCheck } from "ts-exhaustive-check";

interface Square {
  kind: "square";
  size: number;
}

interface Rectangle {
  kind: "rectangle";
  width: number;
  height: number;
}

interface Circle {
  kind: "circle";
  radius: number;
}

type Shape = Square | Rectangle | Circle;

function area(s: Shape) {
  switch (s.kind) {
    case "square":
      return s.size * s.size;
    case "rectangle":
      return s.width * s.height;
    case "circle":
      return Math.PI * s.radius * s.radius;
    default:
      return exhaustiveCheck(s);
  }
}

Without this package the default case in the switch statement would have been this:

function area(s: Shape) {
  switch (s.kind) {
    ...
    default:
      const _exhaustiveCheck: never = s;
      return _exhaustiveCheck;
  }
}

FAQs

Last updated on 10 Dec 2017

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc