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
34 changes: 9 additions & 25 deletions ark/schema/structure/structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ const implementation: nodeImplementationOf<Structure.Declaration> =
const lInner = { ...l.inner }
const rInner = { ...r.inner }
const disjointResult = new Disjoint()
// props an index signature narrows to once the other side's keys
// are known. they can't be added to the side they came from, where
// a prop with the same key would never be merged with them
const derived: nodeOfKind<Prop.Kind>[] = []
if (l.undeclared) {
const lKey = l.keyof()
for (const k of r.requiredKeys) {
Expand All @@ -250,18 +254,8 @@ const implementation: nodeImplementationOf<Structure.Declaration> =
const indexOverlap = intersectNodesRoot(lKey, n.signature, ctx.$)
if (indexOverlap instanceof Disjoint) return []
const normalized = normalizeIndex(indexOverlap, n.value, ctx.$)
if (normalized.required) {
rInner.required = conflatenate(
rInner.required,
normalized.required
)
}
if (normalized.optional) {
rInner.optional = conflatenate(
rInner.optional,
normalized.optional
)
}
derived.push(...(normalized.required ?? []))
derived.push(...(normalized.optional ?? []))
return normalized.index ?? []
})
}
Expand Down Expand Up @@ -289,18 +283,8 @@ const implementation: nodeImplementationOf<Structure.Declaration> =
const indexOverlap = intersectNodesRoot(rKey, n.signature, ctx.$)
if (indexOverlap instanceof Disjoint) return []
const normalized = normalizeIndex(indexOverlap, n.value, ctx.$)
if (normalized.required) {
lInner.required = conflatenate(
lInner.required,
normalized.required
)
}
if (normalized.optional) {
lInner.optional = conflatenate(
lInner.optional,
normalized.optional
)
}
derived.push(...(normalized.required ?? []))
derived.push(...(normalized.optional ?? []))

return normalized.index ?? []
})
Expand All @@ -320,7 +304,7 @@ const implementation: nodeImplementationOf<Structure.Declaration> =
kind: "structure",
baseInner,
l: flattenConstraints(lInner),
r: flattenConstraints(rInner),
r: [...flattenConstraints(rInner), ...derived],
roots: [],
ctx
})
Expand Down
9 changes: 9 additions & 0 deletions ark/type/__tests__/objects/onUndeclaredKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ contextualize(() => {
})
})

it("merges a narrowed index signature with a declared key", () => {
const L = type({ a: "number", "[string]": "number" })
const R = type({ a: "number", b: "number", "+": "reject" })
attest(L.and(R).expression).snap(
"{ a: number, b: number, + (undeclared): reject }"
)
attest(R.and(L).expression).equals(L.and(R).expression)
})

describe("traversal", () => {
const getExtraneousB = () => ({ a: "ok", b: "why?" })

Expand Down
Loading