Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-schema-codegen-optional-tuples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Fix `SchemaRepresentation.toCodeDocument` generating invalid TypeScript for optional tuple elements containing unions or nested readonly tuples. Optional element types are now parenthesized, for example `readonly [(string | number)?]` instead of `readonly [string | number?]`. Generated runtime schemas are unchanged.
2 changes: 1 addition & 1 deletion packages/effect/src/internal/schema/toCodeDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ export function toCodeDocument(
`${element.isOptional ? "Schema.optionalKey(" : ""}${type.runtime}${element.isOptional ? ")" : ""}${
runtimeAnnotate(element.annotations, "annotateKey")
}`,
`${type.Type}${element.isOptional ? "?" : ""}`
element.isOptional ? `(${type.Type})?` : type.Type
)
})
const rest = representation.rest.map((item, index) => recur(item, [...path, "rest", index]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ describe("SchemaRepresentation.toCodeDocument annotations", () => {
},
{
runtime: `Schema.TupleWithRest(Schema.Tuple([Schema.optionalKey(Schema.String)]), [Schema.Number])`,
Type: `readonly [string?, ...Array<number>]`
Type: `readonly [(string)?, ...Array<number>]`
},
{
runtime: `Schema.Struct({ 1: Schema.Boolean })`,
Expand Down
66 changes: 64 additions & 2 deletions packages/effect/test/schema/representation/toCodeDocument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1069,20 +1069,68 @@ describe("toCodeDocument", () => {
assertSchema(
{ schema: Schema.Tuple([Schema.optionalKey(Schema.String)]) },
{
codes: makeCode(`Schema.Tuple([Schema.optionalKey(Schema.String)])`, "readonly [string?]")
codes: makeCode(`Schema.Tuple([Schema.optionalKey(Schema.String)])`, `readonly [(string)?]`)
}
)
assertSchema(
{ schema: Schema.Tuple([Schema.optionalKey(Schema.String)]).annotate({ "description": "a" }) },
{
codes: makeCode(
`Schema.Tuple([Schema.optionalKey(Schema.String)]).annotate({ "description": "a" })`,
"readonly [string?]"
`readonly [(string)?]`
)
}
)
})

it("optional union elements", () => {
assertSchema(
{ schema: Schema.Tuple([Schema.optionalKey(Schema.Union([Schema.String, Schema.Number]))]) },
{
codes: makeCode(
`Schema.Tuple([Schema.optionalKey(Schema.Union([Schema.String, Schema.Number]))])`,
`readonly [(string | number)?]`
)
}
)
assertSchema(
{ schema: Schema.Tuple([Schema.optionalKey(Schema.Literals(["a", "b"]))]) },
{
codes: makeCode(
`Schema.Tuple([Schema.optionalKey(Schema.Literals(["a", "b"]))])`,
`readonly [("a" | "b")?]`
)
}
)
})

it("optional readonly tuple element", () => {
assertSchema(
{ schema: Schema.Tuple([Schema.optionalKey(Schema.Tuple([Schema.String]))]) },
{
codes: makeCode(
`Schema.Tuple([Schema.optionalKey(Schema.Tuple([Schema.String]))])`,
`readonly [(readonly [string])?]`
)
}
)
})

it("optional union element imported from JSON Schema", () => {
assertJsonSchema({
schema: {
type: "array",
prefixItems: [{ anyOf: [{ type: "string" }, { type: "number" }] }],
items: false
}
}, {
codes: makeCode(
`Schema.Tuple([Schema.optionalKey(Schema.Union([Schema.String, Schema.Number.check(Schema.isFinite())]))])`,
`readonly [(string | number)?]`
)
})
})

it("annotateKey", () => {
assertSchema(
{ schema: Schema.Tuple([Schema.String.annotateKey({ "description": "a" })]) },
Expand Down Expand Up @@ -1115,6 +1163,20 @@ describe("toCodeDocument", () => {
})

it("TupleWithRest", () => {
assertSchema(
{
schema: Schema.TupleWithRest(
Schema.Tuple([Schema.optionalKey(Schema.Union([Schema.String, Schema.Number]))]),
[Schema.Boolean]
)
},
{
codes: makeCode(
`Schema.TupleWithRest(Schema.Tuple([Schema.optionalKey(Schema.Union([Schema.String, Schema.Number]))]), [Schema.Boolean])`,
`readonly [(string | number)?, ...Array<boolean>]`
)
}
)
assertSchema(
{ schema: Schema.TupleWithRest(Schema.Tuple([Schema.String]), [Schema.Number]) },
{
Expand Down
Loading