Skip to content

Commit

Permalink
Let TypeScript know about the assertion made
Browse files Browse the repository at this point in the history
This allow TypeScript to know that an assertion was made and that
types can be narrowed down after a call to `invariant()`.

For more details see microsoft/TypeScript#32695
  • Loading branch information
PowerKiKi committed Apr 7, 2020
1 parent a63db21 commit 5a85bed
Show file tree
Hide file tree
Showing 11 changed files with 2,689 additions and 1,595 deletions.
14 changes: 0 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,6 @@ Equivalent to calling `console.warn(...args)`.

The `Error` subclass thrown by failed `invariant` calls.

This class is especially useful when writing TypeScript, because
```ts
invariant(typeof value === "number", "not a number");
console.log(value * 2); // type error!
```
doesn't tell TypeScript anything useful about `value`, whereas the following code can take full advantage of TypeScript's [conditional type narrowing](https://basarat.gitbooks.io/typescript/docs/types/typeGuard.html) functionality:
```ts
if (typeof value !== "number") {
throw InvariantError("not a number");
}
// TypeScript understands that value must be a number here:
console.log(value * 2);
```

### Build-time usage (`rollup-plugin-invariant`)

If you're using [Rollup](https://rollupjs.org) to bundle your code, or using a library that was bundled using Rollup and `rollup-plugin-invariant`, then the above utilities will be transformed so that minifiers can strip the long error strings from your production bundle.
Expand Down
3,117 changes: 1,860 additions & 1,257 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
},
"devDependencies": {
"@types/mocha": "^5.2.6",
"@types/node": "^10.12.20",
"lerna": "^3.13.4",
"typescript": "^3.4.2"
"typescript": "^3.7.0"
}
}
13 changes: 12 additions & 1 deletion packages/rollup-plugin-invariant/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion packages/rollup-plugin-invariant/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import * as recast from "recast";
const b = recast.types.builders;
const { createFilter } = require("rollup-pluginutils");

export default function invariantPlugin(options = {} as any) {
export interface PluginOptions {
errorCodes?: boolean;
importProcessPolyfill?: boolean;
include?: Array<string | RegExp> | string | RegExp | null,
exclude?: Array<string | RegExp> | string | RegExp | null,
}

export default function invariantPlugin(options: PluginOptions = {}) {
const filter = createFilter(options.include, options.exclude);
let nextErrorCode = 1;

Expand Down
6 changes: 2 additions & 4 deletions packages/rollup-plugin-invariant/src/tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "assert";
import fs from "fs";
import plugin from "./plugin";
import plugin, { PluginOptions } from './plugin';
import * as recast from "recast";
import { parse } from "recast/parsers/acorn";
import invariant, { InvariantError } from "ts-invariant";
Expand All @@ -15,9 +15,7 @@ describe("rollup-plugin-invariant", function () {

function check(
id: string,
options?: {
errorCodes: boolean;
},
options?: PluginOptions,
) {
const path = require.resolve(id);
const code = fs.readFileSync(path, "utf8");
Expand Down
Loading

0 comments on commit 5a85bed

Please sign in to comment.