Skip to content

Commit

Permalink
adding jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
alexreardon committed Feb 22, 2024
1 parent 619da0f commit 5459768
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/tiny-invariant.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
const isProduction: boolean = process.env.NODE_ENV === 'production';
const prefix: string = 'Invariant failed';

// Throw an error if the condition fails
// Strip out error messages for production
// > Not providing an inline default argument for message as the result is smaller
/**
* `invariant` is used to [assert](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions) that the `condition` is [truthy](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy).
*
* 💥 `invariant` will `throw` an `Error` if the `condition` is [falsey](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy)
*
* 🤏 `message`s are not displayed in production environments to help keep bundles small
*
* @example
*
* ```ts
* const value: Person | null = { name: 'Alex' };
* invariant(value, 'Expected value to be a person');
* // type of `value`` has been narrowed to `Person`
* ```
*/
export default function invariant(
condition: any,
// Can provide a string, or a function that returns a string for cases where
// the message takes a fair amount of effort to compute
// Not providing an inline default argument for message as the result is smaller
/**
* Can provide a string, or a function that returns a string for cases where
* the message takes a fair amount of effort to compute
*/
message?: string | (() => string),
): asserts condition {
if (condition) {
Expand Down

0 comments on commit 5459768

Please sign in to comment.