make extend dual (#267)

This commit is contained in:
Giulio Canti
2023-05-04 15:39:58 +02:00
committed by GitHub
parent 8012392753
commit ff7d1532bb
3 changed files with 16 additions and 2 deletions
+4
View File
@@ -394,6 +394,10 @@ pipe(
S.extend(S.struct({ c: S.string })),
);
// dual
// $ExpectType Schema<{ readonly a: string; readonly b: string; readonly c: string; }, { readonly a: string; readonly b: string; readonly c: string; }>
S.extend(S.struct({ a: S.string, b: S.string }), S.struct({ c: S.string }));
// $ExpectType Schema<{ [x: string]: string; readonly a: string; readonly b: string; readonly c: string; }, { [x: string]: string; readonly a: string; readonly b: string; readonly c: string; }>
pipe(
S.struct({ a: S.string, b: S.string }),
+7 -2
View File
@@ -875,14 +875,19 @@ const intersectUnionMembers = (xs: ReadonlyArray<AST.AST>, ys: ReadonlyArray<AST
* @category combinators
* @since 1.0.0
*/
export const extend = <IB, B>(that: Schema<IB, B>) =>
<I, A>(self: Schema<I, A>): Schema<Spread<I & IB>, Spread<A & B>> =>
export const extend: {
<IB, B>(that: Schema<IB, B>): <I, A>(self: Schema<I, A>) => Schema<Spread<I & IB>, Spread<A & B>>
<I, A, IB, B>(self: Schema<I, A>, that: Schema<IB, B>): Schema<Spread<I & IB>, Spread<A & B>>
} = dual(
2,
<I, A, IB, B>(self: Schema<I, A>, that: Schema<IB, B>): Schema<Spread<I & IB>, Spread<A & B>> =>
make(
intersectUnionMembers(
AST.isUnion(self.ast) ? self.ast.types : [self.ast],
AST.isUnion(that.ast) ? that.ast.types : [that.ast]
)
)
)
/**
* @category combinators
+5
View File
@@ -3,6 +3,11 @@ import * as S from "@effect/schema/Schema"
import * as Util from "@effect/schema/test/util"
describe.concurrent("extend", () => {
it(`struct extend struct (dual)`, async () => {
const schema = S.extend(S.struct({ a: S.string }), S.struct({ b: S.number }))
await Util.expectParseSuccess(schema, { a: "a", b: 1 })
})
it(`struct with defaults extend struct`, async () => {
const schema = pipe(
S.struct({ a: S.optional(S.string).withDefault(() => ""), b: S.string }),