English
Type generation: give Axon.Xxx real signatures
Axon only knows its controllers at runtime (whatever the page registers), so it is declared as any: Axon.Greeter.greet( completes nothing and mistyped calls compile fine.
axon-gui types reads the runtime registry and emits a type file that closes that gap:
axon-gui types| Direction | Input | Output | What you get in the editor |
|---|---|---|---|
| Python → TS | classes registered with @AxonController + annotations | web/src/axon.generated.ts | Axon.Greeter.greet( completes parameter names and types |
| JS → Python | AxonController.register("DocsJS", { … }) | controller/js_controllers.pyi | api.DocsJS.note( completes in Python |
The first direction is generated by default; the second needs --js (it parses frontend sources with the TypeScript compiler).
Where the files go
One command writes both directions, to these two paths (relative to the project root — it is detected even when you run from web/):
| Artifact | Default path | Override | Notes |
|---|---|---|---|
| Python → TS | web/src/axon.generated.ts | --ts-out <file> | Parent dirs are created; commit it |
| JS → Python | controller/js_controllers.py | --py-out <file> | A runnable .py, not a .pyi |
axon-gui types # both directions (default)
axon-gui types --check # CI: verify both
axon-gui types --only ts # TS only
axon-gui types --ts-out app/types/axon.ts --py-out app/js_api.py # custom paths (--out is an alias)Two hard requirements:
- The TS file must be inside your tsconfig's program (under
src/is easiest). It works throughdeclare module "axon-gui"; if nothing includes it, nothing happens. - The Python file's directory must be importable (give
controller/an__init__.py), becauseconnect()is real runtime code.
What gets generated
python
from axon import AxonController(web_name: str | list[str] = '*', id: Optional[str] = None, shared: bool = False)
``AxonController`` 的命名空间对象:既能当装饰器用,也挂注册表操作。AxonController
@AxonController(web_name: str | list[str] = '*', id: Optional[str] = None, shared: bool = False)
``AxonController`` 的命名空间对象:既能当装饰器用,也挂注册表操作。AxonController("MainView")
class class GreeterGreeter:
counter: intcounter: int = 0
def greet(self, name: str, times: int = 1) -> str:
return "、".join([f"你好,{name}!"] * times)// web/src/axon.generated.ts — generated, do not edit
export interface AxonRegistry {
/** Window: MainView */
Greeter: {
greet(name: string, times?: number): Promise<string>;
counter: number;
};
}
declare module "axon-gui" {
interface AxonControllers extends AxonRegistry {}
}AxonControllers is a typing seam reserved by axon-gui. Without a generated file it is an empty interface and Axon stays any, so this is purely additive — existing projects are unaffected.
Effect
// Before: everything is any
const text = await Axon.Greeter.greet("Axon", 2);
// After: names, types and return values; mistakes fail to compile
const text: string = await Axon.Greeter.greet("Axon", 2);
await Axon.Greeter.greet(42); // ✗ number is not assignable to string
await Axon.Greeter.grete("Axon"); // ✗ no such methodThe async layer is handled too: both def and async def become Promise in JS, so the generated signature is always Promise<ReturnType> and await gives you the value.
Hover to see the generated types
That declare module block is exactly what the generator writes; hover Axon, add and sum to watch the type travel.
ts
import { const Axon: {
DemoController: {
add(a: number, b: number): Promise<number>;
};
}
Axon 的 JS 侧语法糖。
tsawait Axon.Users.add(1, 2) // 调 Python 方法, 位置参数直接摊开
await Axon.Users.slow([1000], {}, 300) // 需要 kwargs / 超时时用打包写法
await Axon.Users.count // 读 Python 属性 (getVar)
Axon.Users.count = 3 // 写 Python 属性 (setVar, fire-and-forget)
属性读是惰性的: 只有真正 await / .then / .catch / .finally 时才发请求。
取到的 dispatcher 本身是函数, 同时挂了 then/catch/finally,
让 Axon.Users.count 既能当 Promise 用, 又能当方法调用。
Axon } from "axon-gui";
declare module "axon-gui" {
interface AxonControllers {
AxonControllers.DemoController: {
add(a: number, b: number): Promise<number>;
}
DemoController: {
function add(a: number, b: number): Promise<number>add(a: numbera: number, b: numberb: number): interface Promise<T>Represents the completion of an asynchronous operation
Promise<number>;
};
}
}
async function function demo(): Promise<string>demo() {
const const sum: numbersum = await const Axon: {
DemoController: {
add(a: number, b: number): Promise<number>;
};
}
Axon 的 JS 侧语法糖。
tsawait Axon.Users.add(1, 2) // 调 Python 方法, 位置参数直接摊开
await Axon.Users.slow([1000], {}, 300) // 需要 kwargs / 超时时用打包写法
await Axon.Users.count // 读 Python 属性 (getVar)
Axon.Users.count = 3 // 写 Python 属性 (setVar, fire-and-forget)
属性读是惰性的: 只有真正 await / .then / .catch / .finally 时才发请求。
取到的 dispatcher 本身是函数, 同时挂了 then/catch/finally,
让 Axon.Users.count 既能当 Promise 用, 又能当方法调用。
Axon.type DemoController: {
add(a: number, b: number): Promise<number>;
}
DemoController.function add(a: number, b: number): Promise<number>add(1, 2);
return const sum: numbersum.Number.toFixed(fractionDigits?: number): stringReturns a string representing a number in fixed-point notation.
toFixed(1);
}When to regenerate
Whenever controller signatures change: new/removed methods, renamed or retyped parameters (defaults become optional), new controllers, changed return types.
axon-gui types # manual
axon-gui types --check # CI: fail when the generated file is staleOr let the Vite plugin do it while you work (dev start and on save, ~150 ms):
import { axonTypes } from "axon-gui/vite-plugin";
export default defineConfig({
plugins: [vue(), axonTypes({ args: ["--controllers", "controller"] })],
});vite build does not run the generator
It needs a Python environment and builds should not. The plugin only attaches to serve; builds use the committed generated file, and CI catches drift with --check.
All three official templates (Vue / React / Angular) ship npm run types and npm run types:check.
Reverse direction: calling frontend code from Python
Frontend controllers are runtime-known too, so Python only gets types where you ask for them. The generator reads:
AxonController.register("DocsJS", {
value: 7,
mirror(payload: { hello: string }, label: string) {
return { mirrored: payload, label };
},
});axon-gui types --js web/srcpython
class class DocsJS(Protocol)DocsJS(Protocol):
value: floatvalue: float
def mirror(self, payload: dict[str, Any], label: str) -> dict[str, Any]: ...
def note(self, text: str) -> None: ...
def def connect(web_name: str) -> AxonJsApiconnect(web_name: str = "*") -> AxonJsApi: ...from controller.js_controllers import connect
api = connect("MainView")
api.DocsJS.note("hello")
mirrored = api.DocsJS.mirror({"hello": "world"}, "tag").result(5)Python has no module augmentation like TypeScript, hence the connect() entry point. Axon(...) itself is still any and you can keep using it.
What it does not do
- No runtime validation — types are editor/compile-time only.
- No annotations, no completion — unannotated members become
any/Any. - Plain JS projects only get method names — parameters degrade to
any. - Cross-window is a union —
AxonCross.web("charts").DocsJSsees every window's controllers, not per-window ones. - Dynamic registration names are skipped — the generator only understands string literals.
Which annotations pay off
| Python | Generated TS |
|---|---|
def f(a: int, b: str = "x") -> bool | f(a: number, b?: string): Promise<boolean> |
def f(items: list[str]) -> dict[str, int] | f(items: Array<string>): Promise<Record<string, number>> |
def f(x: int | None = None) -> None | f(x?: number | null): Promise<null> |
counter: int = 0 | counter: number |
TypedDict / dataclass / pydantic model | inline object { … } |