Metakit / @doeixd/metakit/fn

compose

function · packages/metakit/src/fnBuild.ts:200

function compose<
  P extends ParamSchemas,
  R,
  K extends string,
  S extends SchemaOf,
  R2,
>(
  f: Fn<P, R>,
  g: Fn<Readonly<Record<K, S>>, R2> &
    ([R] extends [ReturnCarrier<S['Type']>] ? unknown : never),
): Fn<P, R2>
function compose<P extends ParamSchemas, R, P2 extends ParamSchemas, R2>(
  f: Fn<P, R>,
  g: Fn<P2, R2>,
  mapArgs: (
    out: ExprType<ParamValues<P>, R>,
    params: ParamPaths<P>,
  ) => {
    readonly [K in keyof P2]: ExprInput<
      ParamValues<P>,
      ReturnCarrier<P2[K]['Type']>
    >
  },
): Fn<P, R2>

Sequential composition (g ∘ f).

  • Unary g (compose(f, g)): feeds f's output into g's sole parameter.
  • Multi-param g (compose(f, g, (out, params) => ({ ... }))): maps f's output and original parameters into g's parameter record.

Preserves f's where clauses, records f's return/ensures and g's parameter Schema checks as scoped intermediate obligations, substitutes g's where, obligations, and ensures over f's parameters, and keeps g's returnSchema.

Strict, as a call is: a call of the composed function has the outcome of g called with f's result, so it refuses where f does even when g's body does not read its parameter.