Skip to content

Commit

Permalink
fix(match): else handler gets records minus those data-matched
Browse files Browse the repository at this point in the history
Previously the `.else` handler in this case would statically receive
`A|B|C`. Now it correctly only receives `B|C`.

```ts
Alge.match(dataABC)
  .A(...)
  .else((data) => ...)
```
  • Loading branch information
jasonkuhrt committed Sep 5, 2022
1 parent c5e19dc commit 734e6a6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Terminology:
*/

import { OmitTag } from './core/types.js'
import { inspect } from './lib/utils.js'
import { inspect, ObjectValues } from './lib/utils.js'
import { SomeRecord } from './record/types/controller.js'
import isMatch from 'lodash.ismatch'

Expand Down Expand Up @@ -151,6 +151,10 @@ type PostMatcher<ADT extends SomeRecord, PreviousTagsMatched extends string, Res
ADT['_tag'] extends PreviousTagsMatched ? {
done: () => Result
} : {
else: <ThisResult extends unknown>(value: ThisResult| ((data: ADT) => ThisResult)) => Result|ThisResult
else: <ThisResult extends unknown>(value: ThisResult | ((data: ExcludeByTag<ADT, PreviousTagsMatched>) => ThisResult)) => Result | ThisResult
}
)

type ExcludeByTag<Record extends SomeRecord, Tag extends string> = Record extends { _tag: Tag }
? never
: Record
6 changes: 5 additions & 1 deletion tests/match/match.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ describe(`.<tag> (Data Matcher)`, () => {
})

describe(`.else`, () => {
it('does not receive tag X when a data matcher for X has been set', () => {
const builder = Alge.match(a as ab).A(() => 1)
builder.else((data) => expectType<B>(data))
})
it(`not available if no matchers have been defined`, () => {
const builder = Alge.match(ab)
// @ts-expect-error Not available yet.
Expand All @@ -119,7 +123,7 @@ describe(`.else`, () => {
it(`is available if some but not all tag matchers have been defined`, () => {
const builder = Alge.match(ab).A(() => 1 as const)
expectType<typeof builder.else>(
0 as any as <ThisResult>(value: ThisResult | ((data: ab) => ThisResult)) => 1 | ThisResult
0 as any as <ThisResult>(value: ThisResult | ((data: B) => ThisResult)) => 1 | ThisResult
)
expect(builder.else).toBeDefined()
})
Expand Down

0 comments on commit 734e6a6

Please sign in to comment.