diff --git a/ark/schema/scope.ts b/ark/schema/scope.ts index 13f5ce013..74e06d1b5 100644 --- a/ark/schema/scope.ts +++ b/ark/schema/scope.ts @@ -755,15 +755,22 @@ export class SchemaScope<$ extends {} = {}> extends BaseScope<$> { const bootstrapAliasReferences = (resolution: BaseRoot | GenericRoot) => { const aliases = resolution.references.filter(node => node.hasKind("alias")) for (const aliasNode of aliases) { - Object.assign(aliasNode.referencesById, aliasNode.resolution.referencesById) + addReferences(aliasNode.referencesById, aliasNode.resolution.referencesById) for (const ref of resolution.references) { if (aliasNode.id in ref.referencesById) - Object.assign(ref.referencesById, aliasNode.referencesById) + addReferences(ref.referencesById, aliasNode.referencesById) } } return resolution } +const addReferences = ( + base: BaseNode["referencesById"], + references: BaseNode["referencesById"] +) => { + for (const id in references) base[id] ??= references[id] +} + const resolutionsToJson = (resolutions: InternalResolutions): JsonStructure => flatMorph(resolutions, (k, v) => [ k, diff --git a/ark/type/__tests__/realWorld.test.ts b/ark/type/__tests__/realWorld.test.ts index fc4c742b1..937710d96 100644 --- a/ark/type/__tests__/realWorld.test.ts +++ b/ark/type/__tests__/realWorld.test.ts @@ -1531,6 +1531,21 @@ date3 must be a parsable date (was "")`) attest(result).equals({}) }) + // https://github.com/arktypeio/arktype/issues/1640 + it("cyclic discriminated union with record reference", () => { + const Node = scope({ + Number: { type: "'number'" }, + Array: { type: "'array'", item: "Node" }, + Unit: { type: "'unit'" }, + Container: { things: "Record" }, + Node: "Number | Array | Unit" + }).export().Node + + const data = { type: "array", item: { type: "number" } } as const + + attest(Node(data)).equals(data) + }) + // https://github.com/arktypeio/arktype/issues/1284 it("cyclic discriminated union issue 4", () => { const ruleset = scope({