Skip to content

Commit

Permalink
Allow Schema.Either to support Never without type errors, closes #3755 (
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored Oct 9, 2024
1 parent 518ef7b commit 360ec14
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
10 changes: 10 additions & 0 deletions .changeset/wet-balloons-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@effect/schema": patch
---

Allow Schema.Either to support Schema.Never without type errors, closes #3755.

- Updated the type parameters of `format` to extend `Schema.All` instead of `Schema<A, I, R>`.
- Updated the type parameters of `Schema.EitherFromSelf` to extend `Schema.All` instead of `Schema.Any`.
- Updated the type parameters of `Schema.Either` to extend `Schema.All` instead of `Schema.Any`.
- Updated the type parameters of `Schema.EitherFromUnion` to extend `Schema.All` instead of `Schema.Any`.
18 changes: 18 additions & 0 deletions packages/schema/dtslint/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2064,6 +2064,12 @@ S.asSchema(S.EitherFromSelf({ right: S.NumberFromString, left: S.String }))
// $ExpectType EitherFromSelf<typeof NumberFromString, typeof String$>
S.EitherFromSelf({ right: S.NumberFromString, left: S.String })

// $ExpectType EitherFromSelf<typeof String$, typeof Never>
S.EitherFromSelf({ right: S.String, left: S.Never })

// $ExpectType EitherFromSelf<typeof Never, typeof String$>
S.EitherFromSelf({ right: S.Never, left: S.String })

// ---------------------------------------------
// Either
// ---------------------------------------------
Expand All @@ -2074,6 +2080,12 @@ S.asSchema(S.Either({ right: S.NumberFromString, left: S.String }))
// $ExpectType Either<typeof NumberFromString, typeof String$>
S.Either({ right: S.NumberFromString, left: S.String })

// $ExpectType Either<typeof String$, typeof Never>
S.Either({ right: S.String, left: S.Never })

// $ExpectType Either<typeof Never, typeof String$>
S.Either({ right: S.Never, left: S.String })

// ---------------------------------------------
// EitherFromUnion
// ---------------------------------------------
Expand All @@ -2084,6 +2096,12 @@ S.asSchema(S.EitherFromUnion({ right: S.NumberFromString, left: S.Boolean }))
// $ExpectType EitherFromUnion<typeof NumberFromString, typeof Boolean$>
S.EitherFromUnion({ right: S.NumberFromString, left: S.Boolean })

// $ExpectType EitherFromUnion<typeof String$, typeof Never>
S.EitherFromUnion({ right: S.String, left: S.Never })

// $ExpectType EitherFromUnion<typeof Never, typeof String$>
S.EitherFromUnion({ right: S.Never, left: S.String })

// ---------------------------------------------
// ReadonlyMapFromSelf
// ---------------------------------------------
Expand Down
14 changes: 7 additions & 7 deletions packages/schema/src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export const asSchema = <S extends Schema.All>(
* @category formatting
* @since 0.67.0
*/
export const format = <A, I, R>(schema: Schema<A, I, R>): string => String(schema.ast)
export const format = <S extends Schema.All>(schema: S): string => String(schema.ast)

/**
* @since 0.67.0
Expand Down Expand Up @@ -6703,7 +6703,7 @@ const eitherParse = <RR, R, LR, L>(
* @category api interface
* @since 0.67.0
*/
export interface EitherFromSelf<R extends Schema.Any, L extends Schema.Any> extends
export interface EitherFromSelf<R extends Schema.All, L extends Schema.All> extends
AnnotableClass<
EitherFromSelf<R, L>,
either_.Either<Schema.Type<R>, Schema.Type<L>>,
Expand All @@ -6716,7 +6716,7 @@ export interface EitherFromSelf<R extends Schema.Any, L extends Schema.Any> exte
* @category Either transformations
* @since 0.67.0
*/
export const EitherFromSelf = <R extends Schema.Any, L extends Schema.Any>({ left, right }: {
export const EitherFromSelf = <R extends Schema.All, L extends Schema.All>({ left, right }: {
readonly left: L
readonly right: R
}): EitherFromSelf<R, L> => {
Expand Down Expand Up @@ -6748,7 +6748,7 @@ const makeRightEncoded = <A>(right: A) => (({
* @category api interface
* @since 0.67.0
*/
export interface Either<R extends Schema.Any, L extends Schema.Any> extends
export interface Either<R extends Schema.All, L extends Schema.All> extends
AnnotableClass<
Either<R, L>,
either_.Either<Schema.Type<R>, Schema.Type<L>>,
Expand All @@ -6761,7 +6761,7 @@ export interface Either<R extends Schema.Any, L extends Schema.Any> extends
* @category Either transformations
* @since 0.67.0
*/
export const Either = <R extends Schema.Any, L extends Schema.Any>({ left, right }: {
export const Either = <R extends Schema.All, L extends Schema.All>({ left, right }: {
readonly left: L
readonly right: R
}): Either<R, L> => {
Expand All @@ -6782,7 +6782,7 @@ export const Either = <R extends Schema.Any, L extends Schema.Any>({ left, right
* @category api interface
* @since 0.67.0
*/
export interface EitherFromUnion<R extends Schema.Any, L extends Schema.Any> extends
export interface EitherFromUnion<R extends Schema.All, L extends Schema.All> extends
AnnotableClass<
EitherFromUnion<R, L>,
either_.Either<Schema.Type<R>, Schema.Type<L>>,
Expand All @@ -6801,7 +6801,7 @@ export interface EitherFromUnion<R extends Schema.Any, L extends Schema.Any> ext
* @category Either transformations
* @since 0.67.0
*/
export const EitherFromUnion = <R extends Schema.Any, L extends Schema.Any>({ left, right }: {
export const EitherFromUnion = <R extends Schema.All, L extends Schema.All>({ left, right }: {
readonly left: L
readonly right: R
}): EitherFromUnion<R, L> => {
Expand Down

0 comments on commit 360ec14

Please sign in to comment.