mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-03 03:25:40 +08:00
make extend dual (#267)
This commit is contained in:
+4
@@ -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 }),
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 }),
|
||||
|
||||
Reference in New Issue
Block a user