Require("openhab").rules is not exposing rule-builder methods / Typescript

Hello,

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.

The solutions I tried are
1)

/**
* @typedef { import("openhab/types/rules/rule-builder").RuleBuilder } RuleBuilder
* @typedef { import("openhab/types/rules/rules") } Rules
* @typedef { RuleBuilder & Rules } RulesT
*/
const ruleBuilder = (/** @type {RulesT} */ (oh.rules))

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
    • Hardware: synology nas
    • OS: linux alpine
    • Java Runtime Environment
    • openHAB version: 3.4M5

Hello @mhauer,

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.

1 Like

Hi @florian-h05,

thanks for your fast response.

Regarding your long todo-list :slight_smile: 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.

Have a nice weekend! Cheers.

Thanks :blush:
Every contributor is welcome, so feel free when you‘ve got the time.

I‘ve opened a doc PR, please have a look at it and tell me if that‘s what you would expect: README: Add note about type defs by florian-h05 · Pull Request #192 · openhab/openhab-js · GitHub.

Also have a nice weekend!!

Looking forward tothe next openHAB Milestone Release which will come on sunday evening.