/** * `composeApi`: the composition root's one primitive. A controller builds * its capability from area slices (each `satisfies Partial`) * plus the service-owned members (authority twins, event hooks); this spreads * them into one object and refuses a key that two slices both define, so a * new verb can never silently shadow an old one. * * const api: FormHostCapability = composeApi('form', [ * fields.api, * values.api, * { canFill: () => authority.can('doc.forms.fill'), onValueChanged: events.valueChanged.on }, * ]); */ type UnionToIntersection = (U extends unknown ? (value: U) => void : never) extends ( value: infer I, ) => void ? I : never; export function composeApi( capability: string, slices: T, ): UnionToIntersection { const seen = new Set(); const api: Record = {}; for (const slice of slices) { for (const key of Object.keys(slice)) { if (seen.has(key)) { throw new Error(`[${capability}] api member '${key}' is defined twice`); } seen.add(key); api[key] = (slice as Record)[key]; } } return api as UnionToIntersection; }