diff --git a/.changeset/thirty-mails-live.md b/.changeset/thirty-mails-live.md new file mode 100644 index 0000000000..abffce371e --- /dev/null +++ b/.changeset/thirty-mails-live.md @@ -0,0 +1,5 @@ +--- +"@effect/schema": patch +--- + +add missing `makePropertySignature` constructor diff --git a/packages/schema/src/Schema.ts b/packages/schema/src/Schema.ts index 0fda2a1728..af83db2ffc 100644 --- a/packages/schema/src/Schema.ts +++ b/packages/schema/src/Schema.ts @@ -1511,8 +1511,7 @@ export interface PropertySignature< ): PropertySignature } -/** @internal */ -export class PropertySignatureImpl< +class PropertySignatureImpl< TypeToken extends PropertySignature.Token, Type, Key extends PropertyKey, @@ -1539,7 +1538,7 @@ export class PropertySignatureImpl< annotations( annotations: PropertySignature.Annotations ): PropertySignature { - return new PropertySignatureImpl(propertySignatureAnnotations_(this.ast, toASTAnnotations(annotations))) + return makePropertySignature(propertySignatureAnnotations_(this.ast, toASTAnnotations(annotations))) } toString() { @@ -1547,6 +1546,21 @@ export class PropertySignatureImpl< } } +/** + * @category PropertySignature + * @since 1.0.0 + */ +export const makePropertySignature = < + TypeToken extends PropertySignature.Token, + Type, + Key extends PropertyKey, + EncodedToken extends PropertySignature.Token, + Encoded, + HasDefault extends boolean = false, + R = never +>(ast: PropertySignature.AST) => + new PropertySignatureImpl(ast) + /** * Lifts a `Schema` into a `PropertySignature`. * @@ -1556,7 +1570,7 @@ export class PropertySignatureImpl< export const propertySignature = ( self: Schema ): PropertySignature, A, never, PropertySignature.GetToken, I, false, R> => - new PropertySignatureImpl(new PropertySignatureDeclaration(self.ast, false, true, {}, undefined)) + makePropertySignature(new PropertySignatureDeclaration(self.ast, false, true, {}, undefined)) /** * Enhances a property signature with a default constructor value. @@ -1599,11 +1613,11 @@ export const withConstructorDefault: { const ast = self.ast switch (ast._tag) { case "PropertySignatureDeclaration": - return new PropertySignatureImpl( + return makePropertySignature( new PropertySignatureDeclaration(ast.type, ast.isOptional, ast.isReadonly, ast.annotations, defaultValue) ) case "PropertySignatureTransformation": - return new PropertySignatureImpl( + return makePropertySignature( new PropertySignatureTransformation( ast.from, new ToPropertySignature(ast.to.type, ast.to.isOptional, ast.to.isReadonly, ast.to.annotations, defaultValue), @@ -1657,7 +1671,7 @@ export const withDecodingDefault: { const ast = self.ast switch (ast._tag) { case "PropertySignatureDeclaration": - return new PropertySignatureImpl( + return makePropertySignature( new PropertySignatureTransformation( ast, new ToPropertySignature(AST.typeAST(ast.type), false, true, {}, undefined), @@ -1666,7 +1680,7 @@ export const withDecodingDefault: { ) ) case "PropertySignatureTransformation": - return new PropertySignatureImpl( + return makePropertySignature( new PropertySignatureTransformation( ast.from, new ToPropertySignature(ast.to.type, false, ast.to.isReadonly, ast.to.annotations, ast.to.defaultValue), @@ -1764,7 +1778,7 @@ export const fromKey: { const ast = self.ast switch (ast._tag) { case "PropertySignatureDeclaration": { - return new PropertySignatureImpl( + return makePropertySignature( new PropertySignatureTransformation( new FromPropertySignature( ast.type, @@ -1780,7 +1794,7 @@ export const fromKey: { ) } case "PropertySignatureTransformation": - return new PropertySignatureImpl( + return makePropertySignature( new PropertySignatureTransformation( new FromPropertySignature( ast.from.type, @@ -1814,7 +1828,7 @@ export const optionalToRequired = ( readonly encode: (ti: TI) => option_.Option } ): PropertySignature<":", TA, never, "?:", FI, false, FR | TR> => - new PropertySignatureImpl( + makePropertySignature( new PropertySignatureTransformation( new FromPropertySignature(from.ast, true, true, {}, undefined), new ToPropertySignature(to.ast, false, true, {}, undefined), @@ -1844,7 +1858,7 @@ export const optionalToOptional = ( readonly encode: (o: option_.Option) => option_.Option } ): PropertySignature<"?:", TA, never, "?:", FI, false, FR | TR> => - new PropertySignatureImpl( + makePropertySignature( new PropertySignatureTransformation( new FromPropertySignature(from.ast, true, true, {}, undefined), new ToPropertySignature(to.ast, true, true, {}, undefined), @@ -2004,7 +2018,7 @@ export const optional: { { decode: option_.filter(Predicate.isNotNull), encode: identity } ) } else { - return new PropertySignatureImpl(new PropertySignatureDeclaration(schema.ast, true, true, {}, undefined)) + return makePropertySignature(new PropertySignatureDeclaration(schema.ast, true, true, {}, undefined)) } } } else { @@ -2062,7 +2076,7 @@ export const optional: { { decode: option_.filter(Predicate.isNotNull), encode: identity } ) } else { - return new PropertySignatureImpl( + return makePropertySignature( new PropertySignatureDeclaration(UndefinedOr(schema).ast, true, true, {}, undefined) ) } diff --git a/packages/schema/test/Schema/PropertySignature.test.ts b/packages/schema/test/Schema/PropertySignature.test.ts index c303f48c53..3569c737d7 100644 --- a/packages/schema/test/Schema/PropertySignature.test.ts +++ b/packages/schema/test/Schema/PropertySignature.test.ts @@ -84,7 +84,7 @@ describe("PropertySignature", () => { }) it("add a decoding default to an optional field", async () => { - const ps: S.PropertySignature<":", number, never, "?:", string, never> = new S.PropertySignatureImpl( + const ps: S.PropertySignature<":", number, never, "?:", string, never> = S.makePropertySignature( new S.PropertySignatureTransformation( new S.FromPropertySignature(S.NumberFromString.ast, true, true, {}, undefined), new S.ToPropertySignature(S.Number.ast, false, true, {}, undefined), @@ -113,7 +113,7 @@ describe("PropertySignature", () => { }) it("add a bidirectional (decoding/encoding) default to an optional field", async () => { - const ps: S.PropertySignature<":", number, never, "?:", string, never> = new S.PropertySignatureImpl( + const ps: S.PropertySignature<":", number, never, "?:", string, never> = S.makePropertySignature( new S.PropertySignatureTransformation( new S.FromPropertySignature(S.NumberFromString.ast, true, true, {}, undefined), new S.ToPropertySignature(S.Number.ast, false, true, {}, undefined), @@ -142,7 +142,7 @@ describe("PropertySignature", () => { }) it("empty string as optional", async () => { - const ps: S.PropertySignature<"?:", string, never, ":", string, never> = new S.PropertySignatureImpl( + const ps: S.PropertySignature<"?:", string, never, ":", string, never> = S.makePropertySignature( new S.PropertySignatureTransformation( new S.FromPropertySignature(S.String.ast, false, true, {}, undefined), new S.ToPropertySignature(S.String.ast, true, true, {}, undefined), @@ -159,7 +159,7 @@ describe("PropertySignature", () => { }) it("encoding default", async () => { - const ps: S.PropertySignature<"?:", number, never, ":", number, never> = new S.PropertySignatureImpl( + const ps: S.PropertySignature<"?:", number, never, ":", number, never> = S.makePropertySignature( new S.PropertySignatureTransformation( new S.FromPropertySignature(S.Number.ast, false, true, {}, undefined), new S.ToPropertySignature(S.Number.ast, true, true, {}, undefined),