Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tidy-cats-match.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Reject unknown properties in `Match.when` object patterns.
9 changes: 9 additions & 0 deletions packages/effect/dtslint/Match.tst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ describe("Match", () => {
})

describe("when", () => {
it("rejects unknown object pattern properties", () => {
pipe(
Match.value(hole<{ _tag: "Transfer"; renamedValue: "foo" } | null>()),
// @ts-expect-error: Type 'string' is not assignable to type 'never'
Match.when({ _tag: "Transfer", value: "foo" }, () => {}),
Match.orElse(() => {})
)
})

it("schema exhaustive-literal", () => {
expect(
pipe(
Expand Down
14 changes: 12 additions & 2 deletions packages/effect/src/Match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ export interface ValueMatcher<in Input, out Filters, out Remaining, out Result,
*/
export type Case = When | Not

type PatternKeys<A> = A extends object ? keyof A : never

type PatternGuard<A, P> = P extends
| Predicate.Predicate<any>
| Predicate.Refinement<any, any>
| SafeRefinement<any>
| ReadonlyArray<any> ? unknown
: P extends object ? { readonly [K in Exclude<keyof P, PatternKeys<A>>]: never }
: unknown

/**
* @category Model
* @since 1.0.0
Expand Down Expand Up @@ -371,7 +381,7 @@ export const when: <
Ret,
Fn extends (_: Types.WhenMatch<R, P>) => Ret
>(
pattern: P,
pattern: P & PatternGuard<R, P>,
f: Fn
) => <I, F, A, Pr>(
self: Matcher<I, F, R, A, Pr, Ret>
Expand All @@ -382,7 +392,7 @@ export const when: <
A | ReturnType<Fn>,
Pr,
Ret
> = internal.when
> = internal.when as any

/**
* Matches one of multiple patterns in a single condition.
Expand Down