studio
studio copied to clipboard
Auto-generate GlobalVariables type in User Scripts
Currently our example User Script requires me to specify the type of global variables up front.
import { Input, Message } from "./types";
// The `./markers` utility provides a helper function to build a Marker.
import { buildRosMarker, MarkerTypes } from "./markers";
type GlobalVariables = { id: number };
export const inputs = ["/input/topic"];
export const output = "/studio_script/my_custom_topic";
// Our node will output a Marker message.
type Marker = Message<"visualization_msgs/Marker">;
// If you want to output multiple markers for a single input message, use a MarkerArray.
// The marker array message has one field, `markers`, which is an array of Marker messaages.
// type MarkerArray = Message<"visualization_msgs/MarkerArray">;
export default function node(event: Input<"/input/topic">, globalVars: GlobalVariables): Marker {
return buildRosMarker({
// Add any fields you want to set in the marker here
// Any fields you omit will use default values
// e.g 'type: MarkerTypes.ARROW' */
});
};
Needing to define the types of variables up front can be pretty confusing to users who are not experienced with TypeScript. I even got tripped up because I didn't see that typedef and was wondering why User Scripts wouldn't let me use any variables not named id
.
A short term fix might be to redefine the example as Record<string, any>
.
A better fix would be auto-generating this type based on the actual set variables. Ideally we still allow unknown properties on this object, so that if I delete a variable it doesn't immediately cause my script to stop compiling.