MCPcopy Create free account
hub / github.com/arktypeio/arktype / intersectPropsAndIndex

Function intersectPropsAndIndex

ark/sets/structure/structure.ts:29–85  ·  view source on GitHub ↗
(
	l: l,
	r: nodeOfKind<"index">,
	$: BaseScope
)

Source from the content-addressed store, hash-verified

27import { intersectNodesRoot } from "../intersections.ts"
28
29const intersectPropsAndIndex = <
30 l extends nodeOfKind<"required"> | nodeOfKind<"optional">
31>(
32 l: l,
33 r: nodeOfKind<"index">,
34 $: BaseScope
35): l | Disjoint | null => {
36 const kind = l.required ? "required" : "optional"
37
38 // a signature reading ctx can only decide a key it's traversing
39 if (r.signature.allowsCtx || !r.signature.allowsUnused(l.key)) return null
40
41 // a key's transforms apply in turn once each value allows its input
42 const intersectValues = (lValue: BaseRoot, rValue: BaseRoot) =>
43 lValue.alters && rValue.alters ?
44 intersectNodesRoot(lValue._in, rValue._in, $)
45 : intersectNodesRoot(lValue, rValue, $)
46 const value = intersectValues(l.value, r.value)
47 // a default is a value of its key, which the signature checks as it would
48 // one the data held
49 if (l.is("optional") && l.hasDef()) {
50 assertDefaultValueAssignability(
51 value instanceof Disjoint ? $ark.kw.never.internal : r.value,
52 l.default,
53 l.key
54 )
55 }
56 // a cyclic value a definition's key meets is decided once definitions close
57 if (
58 kind === "required" &&
59 inProgress.definitions &&
60 !(value instanceof Disjoint) &&
61 value.is("alias")
62 ) {
63 queueUnchecked(() => {
64 const resolved = intersectValues(
65 resolvedRoot(l.value),
66 resolvedRoot(r.value)
67 )
68 if (resolved instanceof Disjoint)
69 resolved.withPrefixKey(l.key, "optional").throw()
70 }, `${l.id}&${r.id}`)
71 }
72 if (value instanceof Disjoint) {
73 // absent, since one of the disjoint values rejects undefined
74 return kind === "optional" ?
75 ($.node(
76 "optional",
77 { key: l.key, value: $ark.kw.never.internal },
78 { reduced: true }
79 ) as l)
80 // optional, since an index signature doesn't require the key
81 : value.withPrefixKey(l.key, "optional")
82 }
83
84 return null
85}
86

Callers 1

structure.tsFile · 0.85

Calls 8

queueUncheckedFunction · 0.90
intersectValuesFunction · 0.85
resolvedRootFunction · 0.85
isMethod · 0.80
withPrefixKeyMethod · 0.80
nodeMethod · 0.80
throwMethod · 0.65

Tested by

no test coverage detected