TypeScript

TypeScript is JavaScript with types. The JavaScript adapter translates a TypeScript script to JavaScript upon saving and executes the result. Nothing changes for the script API.on ,setState ,schedule and everything else is called and works the same way as in JavaScript .

The difference lies before the start. Where JavaScript only reports an error when the affected line is executed – possibly weeks later, in the middle of the night – TypeScript rejects it upon saving.

Invest

When creating a new script, TypeScript is selected as the type. An existing JavaScript script cannot be switched to TypeScript; however, its content can be copied into a new TypeScript script, because valid JavaScript is also valid TypeScript.

What it brings

const id = 'hm-rpc.0.LEQ1234567.1.STATE';

on({ id, change: 'ne', ack: true }, obj => {
    // obj.state.val ist als ioBroker.StateValue bekannt
    const helligkeit: number = obj.state.val as number;
    setState('deconz.0.Lights.1.bri', helligkeit);
});

The editor recognizes the types of the script API and the included libraries. In practical terms, this means that after a period, it suggests what's actually there, a misspelled function name is immediately highlighted, andgetState(...) It doesn't deliver just anything, but a state withval ,ack andts .

Translator settings

The instance settings have a dedicated TypeScript tab where the translation options for all scripts in this instance are set.

The rigorous examination (strict The `state` flag is disabled for scripts, although TypeScript has enabled it automatically since version 6. This is intentional: state values are inherently uncertain, and withstrict Every access toobj.state.val It will be secured. Those who want stricter protection can activate it in this tab.

When it's worth it

TypeScript isn't worthwhile for every three-liner. It's worthwhile when

  • the script is longer than about fifty lines,
  • it is rarely touched and therefore one no longer remembers its construction
  • It works with data structures - lists of rooms, allocation tables, configurations in the script header,
  • An external library is included, the calls to which one does not know by heart.

For the "if motion, then light" scenario, the effort outweighs the benefit. Blockly or a rule-based approach is a better solution.

Please note

  • A translation error is preventing the script from starting. The message appears in the log window below the editor and specifies the line and column.
  • The line numbers in runtime errors refer to the translated code and may differ from the editor.
  • Values from states are initially undefined for TypeScript.as number Or a check in the code is the usual way - and the check is the better answer anyway, because an adapter can certainlynull delivery.