Skip to content

RFC 23: Types across the boundary #24

Open

Description

@yumemi-thomas

One of a series of small proposals, in order; the tracking issue lists them. Depends on: RFC 4 (#5).

Summary

Make rx-tsc type-check .rx code that calls JS libraries, whatever the library's types come from. Four small fixes: the TSX projection writes ref arguments and results the way JS has them (accessors); a JS module with a .d.rx and no .d.ts is typed by its .d.rx; a package declared in rx-types/ (RFC 04) is typed through TypeScript's @types lookup; and a .d.rx matched by tsconfig include no longer crashes rx-tsc.

Motivation

rx-tsc is TypeScript's tsc with .rx modules in the program, through Volar: each .rx module becomes a TSX projection that TypeScript checks. A .d.rx file declares a JS module's exports as rows (export pure fn createDebounced(ref source: string, ms?: number): ref string). In .rx a ref parameter or result is written as a plain value; in JS it is an accessor (() => T).

On upstream:

  • The projection passes the plain value to a ref parameter and binds a ref result as if it were the value. Against the .d.ts projected from the .d.rx that agrees with itself. Against a library's own .d.ts (@tanstack/solid-query takes Accessor<Options>) it is a type error, so a correct TanStack Query app fails rx-tsc.
  • import { useParams } from "./router.js", with router.d.rx beside it and no router.d.ts, is any in TypeScript. The declaration is right there but types nothing.
  • A package that ships no types, declared by the project in rx-types/<pkg>.d.rx (RFC 04), is also any.
  • A tsconfig with "include": ["src"] matches *.d.rx; Volar's tsc stops on a program file it has no script for (upstream Type an entry's default export; stop rx-tsc crashing on .d.rx solidjs/reactivescript#65).

Proposal

TY-4b (projection). A ref argument to a declared JS fn is projected as () => (e). A ref result, or a ref part of a tuple or record result, is read with a call, so the binding has its value's type. The .d.ts projected from a .d.rx gives ref parameters and results accessor types. Both projections (ref/ and the crate) do the same (S-module-13):

-- ./prims.d.rx
export pure fn createDebounced(ref source: string, ms?: number): ref string
-- main.rx
query = createDebounced(props.q, 300)   // projected: createDebounced(() => (props.q), 300)()
twice = query * 2                       // TS2362: query is a string

TY-4b (rx-tsc). When TypeScript asks for router.d.ts, it doesn't exist, and router.d.rx and router.js sit side by side, rx-tsc's file system answers with the .d.ts the crate projects from the .d.rx (rxc serve, declaration). Nothing is written to disk.

MO-8k (rx-tsc). For a package with no types of its own, TypeScript falls back to node_modules/@types/<scope>__<name>/<subpath or index>.d.ts. rx-tsc answers that path in memory with the projection of the package's declaration from rx-types/, a shared @rx-types/ package, or a generated draft. Both projections write such a declaration at that @types path.

.d.rx in include. The language plugin gives a .d.rx an empty TypeScript script (export {};). The .d.rx is still read by the crate beside the modules that import it.

Drawbacks and alternatives

  • rx-tsc answers the virtual files through TypeScript's own host: it wraps ts.sys (fileExists, readFile, realpath, directoryExists, getDirectories) in Volar's runTsc callback, so nothing outside TypeScript sees them. The first prototype patched Node's fs globally; a review pointed out that it reached every module in the process. The alternative is to write .d.ts files to disk, which leaves generated files in the user's tree.
  • Without a .d.ts, a module typed by its .d.rx has nothing else to be checked against (see RFC 24). A hand-written .d.ts would be the same unchecked assertion.
  • The .d.rx-in-include fix is identical to upstream Type an entry's default export; stop rx-tsc crashing on .d.rx solidjs/reactivescript#65's rx-tsc half, so the two merge cleanly.

Prototype

Branch claude/magical-bell-pfhb1w of yumemi-thomas/reactivescript: 3a22ef9 (projection), 9803032 (.d.rx as .d.ts), 151901a (include), d9d2476 (@types fallback in tooling/rx-language-server/src/declarations.js; types_path_of / typesPathOf in both projections), afa696e (patchSystem(ts.sys) in place of the fs patch). Spec: TY-4b, TY-6c, MO-8k; scenario S-module-13. Tests: npm run -s types in ref/, node test/tsc.js in tooling/rx-language-server (a .d.rx in include passes; an rx-types/ package with no types gets TS2345 on the .rx line).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions