Skip to content

Calling JS from SJS

Goal: Call JavaScript libraries safely at the boundary.

Import runtime values normally. Treat unknown shapes as dynamic, then narrow.

Example

import { readFileSync } from "node:fs"

function readJson(path: string): dynamic {
  const text: string = readFileSync(path, "utf8")
  return JSON.parse(text)
}

function getName(doc: dynamic): string? {
  if (doc === null || typeof doc !== "object") return null
  const name: dynamic = doc.name
  return typeof name === "string" ? name : null
}

Open in playground

Key takeaways

  • dynamic replaces TypeScript any.
  • Validate at boundaries — not in hot inner loops.
  • Use @superjs/types-* when available.

Next: dynamic and Schema

Documentation