ioBroker.javascript icon indicating copy to clipboard operation
ioBroker.javascript copied to clipboard

Global TS constants/functions are not defined in JS scripts (again)

Open neopholus opened this issue 4 years ago • 17 comments

I have the same problem as described in #687:

I use global files, to store object ids to being able to switch them out in one place and use the constant in all my scripts.

My global files are all Typescript as well as most of my non-global scripts. This worked in version 4.9.8, but does not work since 4.10.1. (I could not check 4.10.0 as iobroker did not want to install this). The most up-to-date version I tested was 5.0.14, which does also show the described behavior.

Steps to reproduce the behavior:

  • Have a TypeScript file "Konstanten" in global with a constant, e.g.

const ID_DEV_WZ_Fernbedienung_1_PRESS_SHORT:string = getIdByName("WZ.Fernbedienung:1.PRESS_SHORT", true)[0];

  • Add a JavaScript non-global script to any folder and try to use the contstant, e.g.:

on ({id: ID_DEV_WZ_Fernbedienung_1_PRESS_SHORT, change: "any"}, function(){ // do something } );

  • See error
javascript.0 2021-03-14 20:17:44.747 error (16554) ReferenceError: ID_DEV_WZ_Fernbedienung_1_PRESS_SHORT is not defined
javascript.0 2021-03-14 20:17:44.747 error (16554) ^
javascript.0 2021-03-14 20:17:44.747 error (16554) on({ id: ID_DEV_WZ_Fernbedienung_1_PRESS_SHORT, change: "any" }, function () {

Expected behavior The constants should be available in non-global scripts.

Versions: Adapter version: 4.10.1 to 5.0.14 ( I tested versions 4.10.1, 4.10.10, 4.11.0, 5.0.14) JS-Controller version: 3.2.16 Node version: v10.22.0 Operating system: linux

Work around The downgrade to 4.9.8 did fix my problem.

See also #726, which might be related.

neopholus avatar Mar 14 '21 19:03 neopholus

I don't know which of the many PRs from the 4.9-4.10 era causes this, but it has to do with how statements in global TypeScripts are transformed in order to work around the limitation that TypeScript only understands modules, not "scripts". One possible solution in Node.js keeps getting delayed since a few years now.

I can look into another special case for scripts that:

  • only define constants
  • don't define classes, interfaces or types
  • don't use import
  • don't use top-level await

But I don't know if that actually solves the problem in a real-world scenario, where it could actually be easier to move the const definitions into global JavaScript instead.

AlCalzone avatar Mar 14 '21 21:03 AlCalzone

Hi AlCalzone,

thank you for looking into this. According to your hint, I tried to do something else than constants and, thus, I converted the file in a class containing public static variables as I did not want to loose the typing. That works with 5.0.14.

I will try to change this into get accessors, so that the variables cannot be changed, but it is already good enough to work for my use case.

Thanks again!

Stefan

P.S. For everybody who is interested. My constants for my device-ids are now managed inside a class:

class Constants {

    public static get ID_DEV_WZ_Fernbedienung_1_PRESS_SHORT():string { return getIdByName("WZ.Fernbedienung:1.PRESS_SHORT", true)[0]; }
    ....
}

Example for usage:

on ({id: Constants.ID_DEV_WZ_Fernbedienung_1_PRESS_SHORT, change: "any"},
    function() { 
        // do something
    }
); 

neopholus avatar Mar 15 '21 11:03 neopholus

FYI, you don't lose the typing when you switch to JavaScript.

Example: global JS: grafik

non-global TS: grafik

AlCalzone avatar Mar 15 '21 11:03 AlCalzone

Thank you AlCalzone for the explanation. I am not sure if I fully understand your examples as I am still learning JS/TS, but I will try to read a little bit more about typing in JS/TS.

neopholus avatar Mar 18 '21 19:03 neopholus

It was meant to show you that the tooltip contains the correct type without needing to use Typescript.

AlCalzone avatar Mar 18 '21 21:03 AlCalzone

AlCalzone, thank you very much for the explanation! I did not get the fact that these are tooltips! Sorry.

Maybe to prevent problems like this, it would be easier to enforce your strategy to not allow TS scrips as global scripts or at least to show a warning until the solution in Node.js is available? Then everybody can decide if they would like to use this as an unsupported feature or not.

neopholus avatar Mar 21 '21 13:03 neopholus

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs within the next 7 days. Please check if the issue is still relevant in the most current version of the adapter and tell us. Also check that all relevant details, logs and reproduction steps are included and update them if needed. Thank you for your contributions. Dieses Problem wurde automatisch als veraltet markiert, da es in letzter Zeit keine Aktivitäten gab. Es wird geschlossen, wenn nicht innerhalb der nächsten 7 Tage weitere Aktivitäten stattfinden. Bitte überprüft, ob das Problem auch in der aktuellsten Version des Adapters noch relevant ist, und teilt uns dies mit. Überprüft auch, ob alle relevanten Details, Logs und Reproduktionsschritte enthalten sind bzw. aktualisiert diese. Vielen Dank für Eure Unterstützung.

stale[bot] avatar Jun 20 '21 13:06 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs within the next 7 days. Please check if the issue is still relevant in the most current version of the adapter and tell us. Also check that all relevant details, logs and reproduction steps are included and update them if needed. Thank you for your contributions. Dieses Problem wurde automatisch als veraltet markiert, da es in letzter Zeit keine Aktivitäten gab. Es wird geschlossen, wenn nicht innerhalb der nächsten 7 Tage weitere Aktivitäten stattfinden. Bitte überprüft, ob das Problem auch in der aktuellsten Version des Adapters noch relevant ist, und teilt uns dies mit. Überprüft auch, ob alle relevanten Details, Logs und Reproduktionsschritte enthalten sind bzw. aktualisiert diese. Vielen Dank für Eure Unterstützung.

stale[bot] avatar Sep 19 '21 20:09 stale[bot]

I'm having the same problem, but in TS scripts: global/const.ts

const USERDATA = {
  USER_HOME: '0_userdata.0.user_home',
  // ...
};

common/test.ts

console.log(USERDATA);

Results in:

ReferenceError: USERDATA is not defined

Is this the same issue or should I create a new one? (Or am I doing something wrong?)

Edit: Functions are actually working, but constants/variables are not.

binarious avatar Jul 29 '22 17:07 binarious

Hi, something must be really wrong with global scripts. It seems there is a problem in global scripts defined as TS. My examples: Global Script: image

My "common" scripts: JS: image TS: (don't know why console log is not shown in debug window, but in the "Protocol" section, the error is displayed) image

image

Any idea what's wrong? Seems I can't use global TS scripts at all.

Thanks Christof

christofkac avatar Oct 16 '22 10:10 christofkac

which javascript adapter version you use?

Apollon77 avatar Oct 16 '22 10:10 Apollon77

Hi, I use the adapter version 6.0.3. In the debug mode I've observed that only the global scripts with type JavaScript are copied at the beginning at the script. The global Typescripts are not copied at the beginning which explains the behavior. Any idea? Thanks Christof

christofkac avatar Oct 16 '22 20:10 christofkac

Hi, any news on that topic? Thanks

christofkac avatar Oct 22 '22 18:10 christofkac

As you might have guessed of no further actions in the issue the answer is no. Noone had time so far. If you like provide a PR with a fix

Apollon77 avatar Oct 22 '22 18:10 Apollon77

I have this on my to do list but I won't be able to work on it before January

AlCalzone avatar Oct 22 '22 19:10 AlCalzone

Sorry, I thought it might have been not a classical bug as this problem was gone at some point in time. Regaring PR: I contributed in some areas but this problems is a bit too complex to dive into. As it is no showstopper as the work around is to not use TS global scripts, I'll be patient ;-) Thanks for your support!

christofkac avatar Oct 23 '22 09:10 christofkac

Hey, sorry to bump this. I've just upgraded to version 6.1.4 and now my global scripts cannot access each others interfaces, classes and enums anymore.

If there is anything I can do to help resolve this problem, please let me know.

githtz avatar Mar 03 '23 14:03 githtz