/** * Strip TypeScript from a generated browser module. * * A client function's body is emitted verbatim, so anything TypeScript-only * inside one -- an annotated local, an `as` cast, a local interface -- reaches * the browser module as TypeScript source. Codegen removes the types from the * function's *signature*, which is what made this easy to miss: the emitted * module looked transpiled, and only bodies carried types through. * * The artifact is written as `.mjs` and read back as plain JavaScript, so the * failure surfaced as a syntax error pointing at generated code rather than at * the `.wrn` line responsible. */ let transpiler: { transformSync(code: string): string } | null = null; export function stripBrowserTypes(code: string): string { const bun = ( globalThis as unknown as { Bun?: { Transpiler: new (options: unknown) => { transformSync(code: string): string } }; } ).Bun; if (!bun?.Transpiler) { throw new Error( "WRN-CLIENT-TS: emitting a browser module needs the Bun transpiler to remove TypeScript from client function bodies.", ); } transpiler ??= new bun.Transpiler({ loader: "ts", target: "browser" }); return transpiler.transformSync(code); }