diff --git a/ark/schema/scope.ts b/ark/schema/scope.ts index e0128e2fa..13f5ce013 100644 --- a/ark/schema/scope.ts +++ b/ark/schema/scope.ts @@ -181,13 +181,14 @@ const precompile = (references: readonly BaseNode[]): void => const bindPrecompilation = ( references: readonly BaseNode[], - precompiler: CompiledFunction<() => PrecompiledReferences> + precompiler: CompiledFunction<() => PrecompiledReferences>, + owningScope?: BaseScope ): void => { const precompilation = precompiler.write(rootScopeFnName, 4) const compiledTraversals = precompiler.compile()() for (const node of references) { - if (node.precompilation) { + if (node.precompilation && (!owningScope || node.$ !== owningScope)) { // if node has already been bound to another scope or anonymous type, don't rebind it continue } @@ -642,7 +643,7 @@ export abstract class BaseScope<$ extends {} = {}> { if (!this.resolvedConfig.jitless) { const precompiler = precompileReferences(this.references) this.precompilation = precompiler.write(rootScopeFnName, 4) - bindPrecompilation(this.references, precompiler) + bindPrecompilation(this.references, precompiler, this) } this.resolved = true } diff --git a/ark/type/__tests__/realWorld.test.ts b/ark/type/__tests__/realWorld.test.ts index 33539ddc9..4d163aace 100644 --- a/ark/type/__tests__/realWorld.test.ts +++ b/ark/type/__tests__/realWorld.test.ts @@ -1488,6 +1488,31 @@ date3 must be a parsable date (was "")`) attest(result).equals(component) }) + // https://github.com/arktypeio/arktype/issues/1362 + it("cyclic discriminated union with private alias", () => { + const componentModule = type.module({ + "#tabsItem": { + id: "string", + title: "component", + content: "component" + }, + tabs: { + type: "'tabs'", + items: "tabsItem[]" + }, + component: "string | tabs" + }) + const componentSchema = componentModule.component + + const component: typeof componentSchema.infer = { + type: "tabs", + items: [] + } + + // previously threw "TypeError: this.tabs1Apply is not a function" + attest(componentSchema(component)).equals(component) + }) + // https://github.com/arktypeio/arktype/issues/1209 it("cyclic discriminated union issue 3", () => { const $ = scope({