
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@types/invariant
Advanced tools
TypeScript definitions for invariant
The @types/invariant package provides TypeScript type definitions for the invariant npm package. Invariant is a utility that can be used to assert that a certain condition is true. If the condition is false, it will throw an error. The @types/invariant package doesn't contain functionality by itself but adds type support for TypeScript users using the invariant package.
Type Definitions for invariant
This code sample shows how you can use the invariant function with TypeScript type definitions provided by @types/invariant. It asserts that the divisor is not zero before performing the division.
import invariant from 'invariant';
function divide(dividend: number, divisor: number): number {
invariant(divisor !== 0, 'Division by zero.');
return dividend / divisor;
}
divide(10, 2); // works fine
divide(10, 0); // throws error with message 'Division by zero.'
The assert package is a part of Node.js core modules and provides a simple set of assertion tests. It is similar to invariant in that it is used to test expressions for truthiness and throws an AssertionError if the expression evaluates to false. Unlike @types/invariant, assert is not just type definitions but an actual implementation of assertion functions.
Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework. It offers more expressive assertions and expectation methods than invariant. Chai does not need separate type definitions as it includes its own TypeScript support.
Expect.js is a minimalistic BDD-style assertions library that can be used in place of invariant for more expressive assertions. It is similar in purpose but provides a richer API for writing tests. Expect.js does not have separate type definitions and is not as TypeScript-friendly as @types/invariant.
npm install --save @types/invariant
This package contains type definitions for invariant (https://github.com/zertosh/invariant).
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/invariant.
declare let invariant: invariant.InvariantStatic;
export = invariant;
export as namespace invariant;
declare namespace invariant {
interface InvariantStatic {
(testValue: false, format: string, ...extra: any[]): never;
(testValue: any, format: string, ...extra: any[]): asserts testValue;
}
}
These definitions were written by MichaelBennett, dtinth, and Turadg Aleahmad.
FAQs
TypeScript definitions for invariant
We found that @types/invariant demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.