{"id":13451804,"url":"https://github.com/alexreardon/tiny-invariant","last_synced_at":"2025-05-13T20:21:16.932Z","repository":{"id":32079258,"uuid":"129851290","full_name":"alexreardon/tiny-invariant","owner":"alexreardon","description":"A tiny invariant function","archived":false,"fork":false,"pushed_at":"2024-07-25T02:22:37.000Z","size":1219,"stargazers_count":1819,"open_issues_count":21,"forks_count":46,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-27T20:38:05.264Z","etag":null,"topics":["assert","assertion","invariant","type-guard"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexreardon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-04-17T05:33:46.000Z","updated_at":"2025-04-27T08:54:35.000Z","dependencies_parsed_at":"2024-04-30T23:27:46.371Z","dependency_job_id":"1648dc8b-d696-412c-9bca-794506c25d27","html_url":"https://github.com/alexreardon/tiny-invariant","commit_stats":{"total_commits":127,"total_committers":19,"mean_commits":6.684210526315789,"dds":"0.49606299212598426","last_synced_commit":"619da0f9119558cd57aeff1ba5d022cad74f9bc7"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexreardon%2Ftiny-invariant","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexreardon%2Ftiny-invariant/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexreardon%2Ftiny-invariant/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexreardon%2Ftiny-invariant/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexreardon","download_url":"https://codeload.github.com/alexreardon/tiny-invariant/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251311298,"owners_count":21569006,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assert","assertion","invariant","type-guard"],"created_at":"2024-07-31T07:01:03.141Z","updated_at":"2025-04-28T11:52:27.592Z","avatar_url":"https://github.com/alexreardon.png","language":"TypeScript","funding_links":[],"categories":["TypeScript","工具类库","Awesome Tools","JavaScript","Utilities"],"sub_categories":["Languages","Assertions"],"readme":"# tiny-invariant 🔬💥\n\n![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/alexreardon/tiny-invariant/test.yml)\n![types](https://img.shields.io/badge/types-typescript%20%7C%20flow-blueviolet)\n![npm bundle size](https://img.shields.io/bundlephobia/minzip/tiny-invariant)\n![NPM Downloads](https://img.shields.io/npm/dm/tiny-invariant)\n\n`tiny-invariant` is a _tiny_, widely-supported, zero-dependency alternative to [`invariant`](https://www.npmjs.com/package/invariant).\n\n`tiny-invariant` - when every byte counts!\n\n## What is `invariant`?\n\nAn `invariant` function takes a value, and if the value is [falsy](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy) then the `invariant` function will throw. If the value is [truthy](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy), then the function will not throw.\n\n```ts\nimport invariant from 'tiny-invariant';\n\ninvariant(truthyValue, 'This should not throw!');\n\ninvariant(falsyValue, 'This will throw!');\n// Error('Invariant violation: This will throw!');\n```\n\n## Why `tiny-invariant`?\n\nThe [`library: invariant`](https://www.npmjs.com/package/invariant) supports passing in arguments to the `invariant` function in a `sprintf` style `(condition, format, a, b, c, d, e, f)`. It has internal logic to execute the sprintf substitutions. The sprintf logic is not removed in production builds. `tiny-invariant` has dropped all of the code for `sprintf` logic and instead encourages consumers to leverage [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) for message formatting.\n\n```ts\ninvariant(condition, `Hello, ${name} - how are you today?`);\n```\n\n## Error Messages\n\n`tiny-invariant` allows you to pass a `string` message, or a function that returns a `string` message. Using a function that returns a message is helpful when your message is expensive to create.\n\n```ts\nimport invariant from 'tiny-invariant';\n\ninvariant(condition, `Hello, ${name} - how are you today?`);\n\n// Using a function is helpful when your message is expensive\ninvariant(value, () =\u003e getExpensiveMessage());\n```\n\nWhen `process.env.NODE_ENV` is set to `production`, the message will be replaced with the generic message `Invariant failed`.\n\n## Type narrowing\n\n`tiny-invariant` is useful for correctly narrowing types for `flow` and `typescript`\n\n```ts\nconst value: Person | null = { name: 'Alex' }; // type of value == 'Person | null'\ninvariant(value, 'Expected value to be a person');\n// type of value has been narrowed to 'Person'\n```\n\n## API: `(condition: any, message?: string | (() =\u003e string)) =\u003e void`\n\n- `condition` is required and can be anything\n- `message` optional `string` or a function that returns a `string` (`() =\u003e string`)\n\n## Installation\n\n```bash\n# yarn\nyarn add tiny-invariant\n\n# npm\nnpm install tiny-invariant --save\n```\n\n## Dropping your `message` for kb savings!\n\nBig idea: you will want your compiler to convert this code:\n\n```ts\ninvariant(condition, 'My cool message that takes up a lot of kbs');\n```\n\nInto this:\n\n```ts\nif (!condition) {\n  if ('production' !== process.env.NODE_ENV) {\n    invariant(false, 'My cool message that takes up a lot of kbs');\n  } else {\n    invariant(false);\n  }\n}\n```\n\n- **Babel**: recommend [`babel-plugin-dev-expression`](https://www.npmjs.com/package/babel-plugin-dev-expression)\n- **TypeScript**: recommend [`tsdx`](https://github.com/jaredpalmer/tsdx#invariant) (or you can run `babel-plugin-dev-expression` after TypeScript compiling)\n\nYour bundler can then drop the code in the `\"production\" !== process.env.NODE_ENV` block for your production builds to end up with this:\n\n```ts\nif (!condition) {\n  invariant(false);\n}\n```\n\n- rollup: use [rollup-plugin-replace](https://github.com/rollup/rollup-plugin-replace) and set `NODE_ENV` to `production` and then `rollup` will treeshake out the unused code\n- Webpack: [instructions](https://webpack.js.org/guides/production/#specify-the-mode)\n\n## Builds\n\n- We have a `es` (EcmaScript module) build\n- We have a `cjs` (CommonJS) build\n- We have a `umd` (Universal module definition) build in case you needed it\n\nWe expect `process.env.NODE_ENV` to be available at module compilation. We cache this value\n\n## That's it!\n\n🤘\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexreardon%2Ftiny-invariant","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexreardon%2Ftiny-invariant","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexreardon%2Ftiny-invariant/lists"}