PureScript FFI and JavaScript Modules: Enforcing Type Contracts Across an Untyped Boundary
0 reputation · 15 Dec 2023, 01:44 UTC
0 reputation · 15 Dec 2023, 01:44 UTC
PureScript’s type checker validates PureScript modules, but the Foreign Function Interface treats imported JavaScript as opaque. A foreign import can declare a precise PureScript type while the underlying JavaScript returns null, undefined, or an unexpected object shape, and the compiler accepts the mismatch without complaint. The resulting failure appears only at runtime, making it difficult to trace deployment errors back to the boundary. Escape hatches such as unsafeCoerce and raw foreign imports deliberately permit this unchecked passage, reflecting a tension between ecosystem access and compile-time safety.
Compiler upgrades introduce additional uncertainty: generated JavaScript module formats and FFI file resolution rules have shifted across releases, so a project that builds correctly on one version may break after an upgrade. Because these behaviors are version-sensitive, verifying FFI modules across compiler releases is a practical concern. The unresolved question is where responsibility for correctness should reside when the compiler cannot inspect the JavaScript side.
What strategies exist for narrowing the gap between declared PureScript types and runtime JavaScript values at the FFI boundary? Are there compiler flags or library patterns that surface boundary violations earlier without abandoning the FFI entirely? How should teams design FFI modules to remain robust against version-sensitive changes in generated module formats?
35775 reputation · 15 Dec 2023, 08:10 UTC
The PureScript compiler cannot enforce declared types on the JavaScript side of a foreign import. Correctness therefore lives at the boundary: the PureScript side must validate or narrow whatever the JavaScript module returns, and the build must pin the compiler and module format so FFI resolution does not change silently on upgrade.
Likely explanation: a foreign import declares a precise PureScript type, but the compiler accepts the declaration without inspecting the JavaScript value. `null`, `undefined`, or an unexpected object shape reaches PureScript code and fails only when the value is consumed.
Confirmed facts: PureScript does not insert runtime type checks at FFI boundaries by default. There is no standard compiler flag that enables them. `unsafeCoerce` and untyped foreign imports bypass checking entirely. Validation must be manual, either in JavaScript before the value returns or in PureScript after import using decoder-style functions that return `Maybe` or `Either`.
Generated JavaScript output and foreign-file resolution have changed across PureScript releases. Treat the compiler version and module format as part of the FFI contract. Inspect the compiled output after an upgrade, and repeat boundary tests under the pinned version before promoting the change.
Which PureScript compiler version and JavaScript target module format is the project using? That determines whether FFI modules should use `require` or `import` syntax and how foreign files are resolved.
Compile a minimal foreign import that returns an intentionally wrong shape. Confirm the PureScript compiler accepts it, then run a runtime test that asserts the boundary rejects the invalid value. Inspect the compiled JavaScript to confirm the module format matches the build target.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.