{"record":{"id":"5daba246e1eb440a","repo":"can1357/oh-my-pi","slug":"type-fn-requires-a-function-implementation","errorCode":null,"errorMessage":"type.fn requires a function implementation","messagePattern":"type\\.fn requires a function implementation","errorType":"exception","errorClass":"OmpTypeError","httpStatus":null,"severity":"error","filePath":"packages/omptype/src/type.ts","lineNumber":3135,"sourceCode":"\t\t\tconst preceding = definitions.slice(0, spreadIndexes[0]);\n\t\t\tif (\n\t\t\t\tpreceding.some(element => typeof element === \"string\" && (element.endsWith(\"?\") || /\\s=\\s/.test(element)))\n\t\t\t) {\n\t\t\t\tthrow new OmpTypeError(\"A postfix required element cannot follow an optional or defaultable element\");\n\t\t\t}\n\t\t}\n\t\tconst parameterDefinitions = (marker === -1 ? definitions : definitions.slice(0, marker)).map(\n\t\t\tnormalizeFnParameter,\n\t\t);\n\t\tconst params = makeType<readonly unknown[], readonly unknown[]>(parseDef(parameterDefinitions, resolve), [], {});\n\t\tconst returns =\n\t\t\tmarker === -1\n\t\t\t\t? makeType<unknown>({ k: \"unknown\" }, [], {})\n\t\t\t\t: makeType<unknown>(parseDef(definitions[marker + 1], resolve), [], {});\n\t\tconst parameterExpression = fnExpression(params.ir);\n\t\tconst returnsExpression = fnExpression(returns.ir);\n\t\treturn (implementation: (...arguments_: readonly unknown[]) => unknown) => {\n\t\t\tif (typeof implementation !== \"function\") throw new OmpTypeError(\"type.fn requires a function implementation\");\n\t\t\tconst raw = (...arguments_: readonly unknown[]): unknown => {\n\t\t\t\tconst validatedArguments = params.assert(arguments_);\n\t\t\t\tconst result = Reflect.apply(implementation, undefined, validatedArguments);\n\t\t\t\treturn returns.assert(result);\n\t\t\t};\n\t\t\tconst typed = raw.bind(undefined) as TypedFunction<readonly unknown[], unknown>;\n\t\t\tObject.defineProperties(typed, {\n\t\t\t\tname: { value: `bound typed ${implementation.name}`, configurable: true },\n\t\t\t\traw: { value: implementation, enumerable: true },\n\t\t\t\tparams: { value: params, enumerable: true },\n\t\t\t\treturns: { value: returns, enumerable: true },\n\t\t\t\texpression: {\n\t\t\t\t\tvalue: `(${parameterExpression.slice(1, -1)}) => ${returnsExpression}`,\n\t\t\t\t\tenumerable: true,\n\t\t\t\t},\n\t\t\t});\n\t\t\treturn typed;\n\t\t};","sourceCodeStart":3117,"sourceCodeEnd":3153,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/omptype/src/type.ts#L3117-L3153","documentation":"`type.fn(...)` returns a builder that must be invoked with an actual function implementation to produce the validated/typed function. Passing anything that is not a function (undefined, an object, a string) makes wrapping impossible, so omptype throws.","triggerScenarios":"Calling the result of `type.fn(params, returns)` with a non-function value, e.g. `type.fn([\"string\"], \"number\")(undefined)`, or forgetting the final call entirely (the builder itself is passed around as the implementation).","commonSituations":"Destructuring that loses the implementation, conditionally assigning an implementation that ends up undefined, or calling `.fn(...)` in a curry chain and assuming validation happens without the implementation call.","solutions":["Pass a real function to the builder returned by `type.fn`: `const f = type.fn([\"string\"], \"number\")(s => s.length)`.","Check for typos/undefined variables in the implementation argument.","Ensure the implementation is defined before the `type.fn` call executes (no hoisting assumptions on const arrow functions)."],"exampleFix":"// before\nconst parse = type.fn([\"string\"], \"number\"); // never given an implementation\n// after\nconst parse = type.fn([\"string\"], \"number\")(s => s.length);","handlingStrategy":"validation","validationCode":"const impl = getImplementation(); if (typeof impl !== 'function') throw new Error('implementation required'); const safeFn = type.fn(['string'], 'number')(impl);","typeGuard":"const isFn = (v: unknown): v is (...a: readonly unknown[]) => unknown => typeof v === 'function';","tryCatchPattern":"try { const f = type.fn(params, returns)(impl); } catch (e) { if (e instanceof OmpTypeError) reportMissingImplementation(); else throw e; }","preventionTips":["Always invoke the builder returned by type.fn in the same expression","Check the implementation variable is defined (not undefined via circular import) before calling","Do not pass the builder itself where a function value is expected"],"tags":["function-schema","validation","api-misuse"],"backgroundTag":"missing-function-implementation","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}