Skip to content

fix(type): wire up type.fn.raw at runtime#1631

Open
aarsh767 wants to merge 1 commit into
arktypeio:mainfrom
aarsh767:fix-fn-raw-runtime
Open

fix(type): wire up type.fn.raw at runtime#1631
aarsh767 wants to merge 1 commit into
arktypeio:mainfrom
aarsh767:fix-fn-raw-runtime

Conversation

@aarsh767

Copy link
Copy Markdown

Closes #1609

type.fn.raw is typed as a function (an alias of fn with no type-level validation), but at runtime it's always undefined, so calling it throws type.fn.raw is not a function:

import { type } from "arktype"

type.fn.raw() // throws: type.fn.raw is not a function

Cause

InternalFnParser builds its attach object with raw: $.fn, but $.fn is the very InternalFnParser instance being constructed (scope.fn = new InternalFnParser(this)), so it's still undefined at that point. Callable applies attach with Object.assign, which copies the value, so raw permanently captures undefined (a lazy getter wouldn't help either, since Object.assign evaluates it).

Fix

Extract the parser body into a local parse function and reference it for both the callable body and raw. This removes the dependency on the half-initialized scope — raw and fn now point at the same parser, which matches the documented "alias of fn" behavior.

Tests

Added a raw case to ark/type/__tests__/fn.test.ts asserting it parses, runs, and validates like fn (including the same traversal error on bad input).

type.fn.raw was typed but always undefined at runtime, so calling it threw
"type.fn.raw is not a function".

The parser read `$.fn` while building its `attach` object, but `$.fn` is the
InternalFnParser instance currently being constructed, so it was still
undefined and got captured as `raw`. Extract the parser into a local `parse`
function and reference it for both the callable body and `raw` so there's no
longer a dependency on the half-initialized scope.

Closes arktypeio#1609

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes — a runtime fix for type.fn.raw and a regression test.

  • Extract the parser body into a local parse function in InternalFnParser and use it as both the callable body and the raw attachment, avoiding the uninitialized $.fn reference.
  • Add a raw test case to ark/type/__tests__/fn.test.ts asserting it parses, runs, and validates like fn.

Pullfrog  | View workflow run | Using Kimi K2 (free via Pullfrog for OSS) | 𝕏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To do

Development

Successfully merging this pull request may close these issues.

type.fn.raw incorrectly undefined (exists only at type-level)

2 participants