This is a project for converting typescript type definitions to Python type stubs for use with Pyodide.
It uses the typescript compiler to generate a typescript abstract syntax tree for the types, converts the typescript ast into an IR, then it renders this IR into Python type hints. The most complicated step is converting the typescript ast to the IR.
For the DOM it is also possible to generate type hints from the webidl.
Webtypy ingests the webidl to produce type
stubs for the Pyodide js module. ts-to-python produces the type stubs from
TypeScript .d.ts files. The webidl and TypeScript type declarations contain
different information. The ideal approach would combine both, but it will take
some effort to implement.
Of course many more projects have typescript type hints but no idl definitions so the typescript approach has much broader applicability.
The webidls distinguish between floating point and integer types, like Python
but unlike TypeScript. Using .d.ts files as our source of truth, we have no
choice but to use float | int as the number type everywhere.
TypeScript declarations include types for ECMAScript builtins. They are also available for Node and for many other packages.
You can run the TypeScript unit tests for the type generation with:
npm run -C type-generation testTo run the Python tests for the generated types, first you have to generate the types with:
./generate_types.shThen run the tests as follows:
cd mypy-tests
uv run pytest