Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving Predicate Usability of Schema.is #2756

Merged
merged 1 commit into from
May 16, 2024
Merged

Conversation

gcanti
Copy link
Contributor

@gcanti gcanti commented May 16, 2024

Before this update, the Schema.is(mySchema) function couldn't be easily used as a predicate or refinement in common array methods like filter or find. This was because the function's signature was:

(value: unknown, overrideOptions?: AST.ParseOptions) => value is A

Meanwhile, the function expected by methods like filter has the following signature:

(value: unknown, index: number) => value is A

To make Schema.is compatible with these array methods, we've adjusted the function's signature to accept number as a possible value for the second parameter, in which case it is ignored:

-(value: unknown, overrideOptions?: AST.ParseOptions) => value is A
+(value: unknown, overrideOptions?: AST.ParseOptions | number) => value is A

Here's a practical example comparing the behavior before and after the change:

Before:

import { Schema } from "@effect/schema"

declare const array: Array<string | number>

/*
Throws an error:
No overload matches this call.
...
Types of parameters 'overrideOptions' and 'index' are incompatible.
*/
const strings = array.filter(Schema.is(Schema.String))

Now:

import { Schema } from "@effect/schema"

declare const array: Array<string | number>

// const strings: string[]
const strings = array.filter(Schema.is(Schema.String))

Note that the result has been correctly narrowed to string[].

@gcanti gcanti added the schema label May 16, 2024
Copy link

changeset-bot bot commented May 16, 2024

🦋 Changeset detected

Latest commit: 6f23303

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 18 packages
Name Type
@effect/schema Patch
@effect/cli Patch
@effect/experimental Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/platform Patch
@effect/rpc-http Patch
@effect/rpc Patch
@effect/sql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gcanti gcanti merged commit ee08593 into main May 16, 2024
12 checks passed
@gcanti gcanti deleted the schema/is-as-predicate branch May 16, 2024 05:56
@github-actions github-actions bot mentioned this pull request May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

1 participant