ROS TypeScript Generator
ros-typescript-generator is a Command Line Interface (CLI) tool designed to generate TypeScript interfaces and enums directly from ROS (Robot Operating System) message (`.msg`), service (`.srv`), and action (`.action`) definition files. Currently at version 1.10.0, this package sees active development with several minor releases annually, often introducing new features and bug fixes. A key differentiator is its focus on generating pure TypeScript types without any runtime dependencies or classes in the output, making it an ideal choice for frontend applications or any project requiring only type definitions. It supports both ROS1 and ROS2 message formats and offers configurable output options like type prefixes, namespaces, and 'smart enums', setting it apart from alternatives that might include Node.js-specific runtime components.
Common errors
-
Error: ENOENT: no such file or directory, stat '/opt/ros/iron/share/std_msgs'
cause The generator could not find one of the ROS package directories specified in the `input` array of your `ros-ts-generator-config.json`.fixDouble-check the `path` values in your configuration file. Ensure they are correct, absolute paths to existing ROS share directories, and that you have read permissions. -
Error: No config file found at specified path.
cause The `ros-typescript-generator` CLI was invoked but could not locate the configuration file at the path provided by the `--config` argument.fixVerify that the `ros-ts-generator-config.json` file exists at the specified location and that the `--config` flag includes the correct full path and filename, e.g., `npx ros-typescript-generator --config ./ros-ts-generator-config.json`. -
Error: Invalid ROS version '0' specified in config. Must be 1 or 2.
cause The `rosVersion` property in your `ros-ts-generator-config.json` is set to an invalid value (e.g., `0`, `3`, or a string).fixCorrect the `rosVersion` entry in your configuration file to either `1` for ROS1 or `2` for ROS2.
Warnings
- breaking Version 1.8.0 introduced a change where service messages are bundled into a single message type containing distinct `Request` and `Response` sub-interfaces. This significantly alters the generated type structure for services.
- gotcha Incorrect or inaccessible paths specified in the `input` array of your configuration file will cause the generation process to fail silently or with an error, as the CLI cannot locate the ROS message files.
- gotcha The `smartEnums` feature, introduced in v1.9.0, changes how constants within `.msg` files are interpreted. When enabled, it attempts to infer distinct enum types, which might lead to unexpected enum generations if your constant naming conventions are ambiguous.
- gotcha The `rosVersion` field in the `ros-ts-generator-config.json` is mandatory and must be explicitly set to either `1` or `2`. Omitting it or providing an invalid value will result in a generation error.
Install
-
npm install ros-typescript-generator -
yarn add ros-typescript-generator -
pnpm add ros-typescript-generator
Imports
- generate
import { generate } from 'ros-typescript-generator' - CliConfig
import { CliConfig } from 'ros-typescript-generator' - RosVersion
import { RosVersion } from 'ros-typescript-generator'
Quickstart
{
"output": "./generated/ros_msgs.ts",
"rosVersion": 2,
"typePrefix": "IRosType",
"useNamespaces": false,
"smartEnums": true,
"input": [
{
"namespace": "std_msgs",
"path": "/opt/ros/iron/share/std_msgs"
},
{
"namespace": "geometry_msgs",
"path": "/opt/ros/iron/share/geometry_msgs"
}
]
}
// 1. Save the above JSON content into a file named `ros-ts-generator-config.json` in your project root.
// 2. Run the generator using npx:
npx ros-typescript-generator --config ros-ts-generator-config.json
// 3. Example of how to use the generated types in your TypeScript project (e.g., in `src/app.ts`):
// import { IRosTypeStdMsgsHeader } from './generated/ros_msgs';
// const myHeader: IRosTypeStdMsgsHeader = {
// seq: 10,
// stamp: { sec: 1678886400, nanosec: 500000000 },
// frame_id: 'robot_base'
// };
// console.log('Generated ROS Header:', myHeader);