Skip to content

Couldn't import the type definitions of std_msgs in my Typescript program #1089

@A-Marge-SSCI

Description

@A-Marge-SSCI

Description

I seem to be unable to import the type definitions of std_msgs in my Typescript program. I am new to typescript/javascript, but I think I've checked the obvious stuff:

My tsconfig.json file contains the following:

...
  "compilerOptions": {
   ...
    "module": "commonjs",
    "moduleResolution": "node",
    "target": "es6"
    },
...

I ran npx generate-ros-messages and in <yourproject>/node_modules/rclnodejs/generated/ I see a std_msgs directory with files for every type, including strings.
I also have an interfaces.d.ts file located in <yourproject>/node_modules/types, and it contains:

...
declare module 'rclnodejs' {
...
  namespace std_msgs {
  ...
    namespace msgs {
    ...
      export interface String {
      ...

I'm just not sure why I can't access the rclnodejs.std_msgs.msg.String type directly, do I have to separately include interface.d.ts?

  • Library Version: "rclnodejs": "0.27.4"
  • ROS Version: Ros2 - Humble
  • Platform / OS: Description: Ubuntu 20.04.6 LTS

Steps To Reproduce

  1. Start the following typescript code using npx tsx src/example.ts
  2. Send a ros2 message to the /msg_sub topic using ros2 topic pub /msg_sub std_msgs/msg/String "data: hello"
import * as rclnodejs from 'rclnodejs';

async function main() {
  await rclnodejs.init();

  const node = new rclnodejs.Node('ros2_pub_ex');
  const publisher = node.createPublisher('std_msgs/msg/String', 'msg_pub');
  node.createSubscription('std_msgs/msg/String', 'msg_sub', (msg) => {
    if ( msg instanceof rclnodejs.std_msgs.msg.String) {
      console.log(`Received std_msgs/msg/String with data: ${msg.data}`)
    }
  });
  node.spin();

  let num = 0;
  setInterval(() => {
      let stringMsgObject = rclnodejs.createMessageObject('std_msgs/msg/String');
      stringMsgObject.data = `hello world ${num++}`;
      console.log('publishing ', stringMsgObject);
      publisher.publish(stringMsgObject);
  }, 1000);
}

void main();

process.on('SIGINT', () => process.exit(1));

Expected Behavior

I expected a console printout of receiving the message. Ex:
Received std_msgs/msg/String with data: hello

Actual Behavior

/workspaces/rclnodejs_example/src/example.ts:10
    if ( msg instanceof rclnodejs.std_msgs.msg.String) {
                                           ^

TypeError: Cannot read properties of undefined (reading 'msg')

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions