$ show ocsigen/ts2ocaml
ts2ocaml is a CLI tool that generates OCaml and ReScript bindings from TypeScript type definitions. It parses TypeScript AST using the TypeScript compiler API (via Fable), transforms the AST into a language-agnostic internal representation, resolves types (including generics, subtyping, unions, intersections), and emits target-specific code for js_of_ocaml (.mli) or ReScript (.res/.resi). The tool is implemented in F#, compiled to JavaScript via Fable, and distributed as an npm package. It supports multiple backends via a pluggable ITarget interface, includes a comprehensive type resolver (Typer), and provides utilities for naming, text manipulation (rope), trie-based data structures, and dependency graph analysis (topological sort, SCC). The repository also ships a minimal standard library for each target to support emitted bindings. It is actively maintained with CI, frequent releases, and documentation for modeling subtyping and development setup.
The LLM identified 5 files worth highlighting for reuse:
A generic prefix tree (trie) implementation used for structured text output and dependency tracking. It provides efficient insertion, lookup, and prefix-based iteration. The implementation is self-contained and can be extracted for any project needing a trie data structure.
A generic directed graph module with topological sort and strongly connected components (SCC) algorithms. Used to order module dependencies for code generation. The module is independent of the rest of the codebase and can be reused in any F# project requiring graph analysis.
A rope data structure for efficient string concatenation and manipulation. Provides operations like append, prepend, substring, and concatenation with lazy evaluation. Suitable for large text construction scenarios such as code generation. Self-contained and reusable.
A utility module for converting identifier naming conventions (camelCase, snake_case, PascalCase), escaping reserved keywords, and inferring module names from JavaScript module paths. It is independent of the rest of the codebase and can be extracted for any project needing naming transformations.
Defines the ITarget interface with members Command, Description, Builder, and Run, enabling a plugin-like architecture for adding new output language targets. This pattern decouples the core transformation pipeline from code generation, making it easy to extend the tool without modifying core logic.