I am just getting started to shift my DSL-rules to JS Scripting with openhab-js. In general it seems to work fine. But the types are not recognized correctly on openhab.rules.*. Only the exported symbols of /rules/rules.js are exposed and in turn suggested by the IDE(vs-code).
My guess is, that the export from /types/openhab-js.d.ts export const rules: typeof import("./rules/rules");
is invalid.
Changing the export in openhab-js.d.ts (above) to export const rules: typeof import("./rules/rules") & typeof import("./rules/rule-builder");
This at least gave me the rule-builder members (e.g. when). But nevertheless I don’t get any further suggestions from the IDE after ruleBuilder.when().*
In general I would be very interested if there is planned support for ES6 module import and .ts files.
Maybe I am using it wrong. Any help would be appreciated.
Cheers,
Michael
Platform information: openhab:milestone in docker container
at the openhab-js repo, where the JavaScript library is developed, we (the maintainers) are aware of this issue. Currently, we are missing the type definitions for the RuleBuilder API, but the rest of the library should be covered.
The problem why we haven‘t got type defs for RuleBuilder yet, is the complexity of RuleBuilder, for which I honestly haven‘t found the time yet to add type defs for it.
Just changing the export statement for the type defs is no solution, since your change would disable type definitions for JSRule, runRule, and so on.
About ES6 import: I am not sure if that would work because ES6 import is working asynchronously, and there are limitations which do not allow us to run async tasks. I can probably have a look if we can get ES import working, however this is not high on my (long) priority list.
EDIT: Seems like GraalJS (the JavaScript runtime we use) supports ES6 import, so it may be possible to support this in openHAB.
Support for .ts files is something we definitely will not be able to provide, since TypeScript needs the TypeScript compiler to „compile“ to pure JavaScript. If you want typescript, you can however write your ts code somewhere and then „compile“ it and copy this to openHAB.
Note, that if you want more strict type behaviour in VS Code, you can add // @ts-check to the top of your js files.
Regarding your long todo-list I appreciate your and all the community-members efforts on making openhab great. Maybe I can myself find the time to get into contribution someday.
Referring to ES6 support I already did some research and already saw that it supports ES6 (and there are interesting discussions about direct Typescript compilation too). But I also think about your suggestion to build the rules elsewhere and then include the compiled code to /automation/js.
However, what I found very difficult was that I spent a couple of hours analyzing this. My suggestion is to add a short comment in the Rule-Builder section of JS Scripting docs that TS-types are not available.