diff --git a/src/tiny-invariant.flow.js b/src/tiny-invariant.flow.js index 0f534cc..a8175c8 100644 --- a/src/tiny-invariant.flow.js +++ b/src/tiny-invariant.flow.js @@ -4,6 +4,23 @@ const prefix: string = 'Invariant failed'; +/** + * __invariant__ + * + * The `invariant` function takes a value, and if the value is falsy then the `invariant` function will throw. If the value is truthy, then the function will not throw. + * + * @param {boolean} condition A boolean condition - if falsey will thrown an error. + * @param {(string|() => string)} message The message provided to accompany the invariant. Can provide a string, or a function that returns a string for cases where the message takes a fair amount of effort to compute. + * + * @returns {asserts condition is true} + * @example + * ```tsx + * import invariant from 'tiny-invariant'; + * + * // throws when 1 no longer equals 1 + * invariant(1 === 1, 'Maths is broken'); + * ``` + */ export default function invariant(condition: mixed, message?: string | (() => string)) { if (condition) { return; diff --git a/src/tiny-invariant.ts b/src/tiny-invariant.ts index 6c82392..6c42275 100644 --- a/src/tiny-invariant.ts +++ b/src/tiny-invariant.ts @@ -1,13 +1,25 @@ 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__ + * + * The `invariant` function takes a value, and if the value is falsy then the `invariant` function will throw. If the value is truthy, then the function will not throw. + * + * @param {*} condition A boolean condition - if falsy will throw an error. + * @param {(string|() => string)} message The message provided to accompany the invariant. Can provide a string, or a function that returns a string for cases where the message takes a fair amount of effort to compute. + * + * @returns {asserts condition is true} Asserts condition is truthy. + * @example + * ```tsx + * import invariant from 'tiny-invariant'; + * + * // throws when 1 no longer equals 1 + * invariant(1 === 1, 'Maths is broken'); + * ``` + */ 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 message?: string | (() => string), ): asserts condition { if (condition) {