Type the stubs of imported JavaScript modules from TypeScript and JSDoc
Type of change
- New feature (non-breaking change which adds functionality)
- This change requires a documentation update
Description
Third of the ENG-9127 stack, on top of reflex-dev/reflex#7950.
Problem. Python can now import a JavaScript module's functions, but nothing checks the calls. A wrong argument type, or a missing argument, shows up only in the browser.
Change. In dev mode, Reflex writes a type stub (party.pyi) next to each imported JS/TS module, so pyright and mypy check every call:
// my_app/scripts/typed.ts
export function setText(elementId: string, count: number): void { ... }
export function flags(elementId: string, on: boolean, tags: string[]): void { ... }
becomes
def setText(elementId: str | Var[str] | EventCallback[...], count: float | Var[float], /) -> EventSpec: ...
def flags(elementId: str | Var[str] | EventCallback[...], on: bool | Var[bool], tags: Sequence[str] | Var[Sequence[str]], /) -> EventSpec: ...
Argument types come from TypeScript annotations or, in .js files, from JSDoc. The TypeScript checker classifies each type. Nothing is parsed out of printed type strings.
| TypeScript | Python |
|---|---|
string | str (also accepts an event handler, which is sent as its name) |
number / boolean | float / bool |
T[], tuples | Sequence[...] |
Record<string, T> | Mapping[str, T] |
| literal unions, enums | Literal[...] |
Set, Map, bigint, Promise, functions, classes, DOM types, untyped | Any, because arguments arrive as JSON |
Rules.
- Which functions a stub declares. Exactly the functions #7950's compile-time parser reports for the module, the same list Reflex checks Python's names against. So a stub never lets pyright accept a name that fails at compile or in the browser. TypeScript only adds types to those names. A function it cannot type takes
(*args: Any). Names re-exported withexport *are not declared; re-export them by name. - Dev only. Stubs are written only in dev mode. Production builds write none. Typescript is installed in every mode once the app imports a JS module, so the committed
reflex.lock/is the same afterreflex runandreflex export. It never reaches the browser bundle. - No restarts. The
.pyiextension is ignored by hot reload, so writing a stub doesn't restart the backend. - Commit them. The docs recommend committing stubs, so type checking works in CI without Node.
- When a stub is rewritten. Only when its text changes. The first line is a fixed marker,
# Generated by Reflex from party.ts; do not edit., so a Reflex upgrade, a port change or a fresh clone rewrites nothing whose declarations are the same. What decides whether TypeScript runs again (the module's files, the app files and package.json of the packages its types read, the Reflex version and the generator) is cached in.web/script_stubs.json, not in the committed stub. A stub without the marker is the user's and is never touched. To regenerate one by hand, delete it. - Cleanup. A stub Reflex wrote for a module the app no longer imports is removed. A hand-written
.pyiis never touched. - Strict-clean. A stub imports only the names it uses, and passes pyright in strict mode.
- Type-only files. Type-only references (JSDoc
import("…"),/// <reference>,import type,.d.ts) count toward the stub but are never copied into.web, and never raise the outside-the-package error.
Design. script-signatures.js stays separate from #7950's script-modules.js. It needs typescript and is slow, so it runs only for modules whose digest changed. A steady compile doesn't start it: 0.47 s against 1.07 s when it re-reads types. Merging the two would load typescript on every compile, production included.
Warnings. Each states the problem, its cause and the fix. When the types can't be read, the stubs accept any arguments and the app still runs:
Reflex could not read the argument types of the JavaScript modules …, so their type stubs accept any arguments: script-signatures.js failed: …. The app still runs. If this repeats, delete …/node_modules so that the next run reinstalls typescript, or report the error at https://github.com/reflex-dev/reflex/issues.
Tests
- Unit: the Python renderer, run on the real output of
script-signatures.jsfrom a fixture that vitest also checks. The failure paths run a real subprocess: a non-zero exit and a timeout. The digest is compared exactly, orphaned stubs are removed, and a production compile writes no stub. One test runs both real scripts from a.tsand a JSDoc module through to the stub. It needs the frontend packages, so it is skipped where they are not installed. - vitest:
script-signatures.jsis fully covered. - Playwright
test_call_script.py: the stub generated for a module is checked against its exact text. - Coverage: 100% of the added Python lines and branches and JS statements. ruff, pyright and
npm run checkare clean.