Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
@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
The npm package @types/invariant receives a total of 632,496 weekly downloads. As such, @types/invariant popularity was classified as popular.
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
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.