Skip to content

Commit

Permalink
Merge pull request #175 from gvergnaud/gvergnaud/fix-re-export-of-pat…
Browse files Browse the repository at this point in the history
…terns

🐛 fix: Allow re-exporting patterns from ES Modules
  • Loading branch information
gvergnaud committed Jul 14, 2023
2 parents 8e9e330 + 109d8c1 commit a612fa2
Show file tree
Hide file tree
Showing 7 changed files with 597 additions and 522 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,20 @@ const posts = await fetch(someUrl)

Although not strictly necessary, using `as const` after the pattern definition ensures that TS-Pattern infers the most precise types possible.

### `P.narrow`

`P.narrow<Input, typeof Pattern>` will narrow the input type to only keep the set of values that are compatible with the provided pattern type.

```ts
type Input = ['a' | 'b' | 'c', 'a' | 'b' | 'c'];
const Pattern = ['a', P.union('a', 'b')] as const;

type Narrowed = P.narrow<Input, typeof Pattern>;
// ^? ['a', 'a' | 'b']
```

Note that most of the time, the `match` and `isMatching` functions perform narrowing for you, and you do not need to narrow types yourself.

### `P.Pattern`

`P.Pattern<T>` is the type of all possible pattern for a generic type `T`.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-pattern",
"version": "5.0.2",
"version": "5.0.3",
"description": " The exhaustive Pattern Matching library for TypeScript.",
"type": "module",
"source": "src/index.ts",
Expand Down
1 change: 1 addition & 0 deletions src/internals/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type unset = typeof unset;
export const isVariadic = Symbol.for('@ts-pattern/isVariadic');
export type isVariadic = typeof isVariadic;

// can't be a symbol because this key has to be enumerable.
export const anonymousSelectKey = '@ts-pattern/anonymous-select-key';
export type anonymousSelectKey = typeof anonymousSelectKey;

Expand Down

0 comments on commit a612fa2

Please sign in to comment.