TypeScript

Tabris.js 3 targets TypeScript 2.9 or later. TypeScript is a type-safe dialect of JavaScript/EcmaScript and also supports ES6 module syntax (import and export statements) and async/await. A complete guide to TypeScript can be found at typescriptlang.org. As an IDE we can recommend Visual Studio Code with the tslint extension, but there are many suitable TypeScript IDEs out there.

TypeScript files have to be compiled to JavaScript before execution. The compiler is installed in the project directory to node_modules/.bin/tsc when generating a new Tabris.js app using the tabris init command. To compile the code, simply type npm run build. This also happens during a full app build via tabris build or using the build service. Alternatively, type npm start to start the compiler in watch mode for incremental compiling and code side loading.

Type Safety

In TypeScript not all APIs, not even all Tabris.js APIs, are perfectly type safe. Here are some guidelines to avoid losing type safety.

Tabris.js specific recommendations:

  • Do not use the widget.on() method. Instead, the Listeners API, e.g. widget.onResize(cb);.
  • Create type-aware WidgetCollection instances using widget constructors as selectors, e.g. widget.children(Button).first();.
  • Be aware that while widget.set() and widget constructors provides auto-completion it does not prevent you from setting non-existing properties.
  • Use widget.apply() only to set properties of the base Widget class, like layoutData. Or avoid it completely.

General TypeScript recommendations:

  • Avoid any. An implicit any may occur if you do not give a type for a variable, field or parameter, and none can be inferred by assignment.
  • Avoid explicit casting, Use type guards instead.
  • When using third-party libraries you may have to manually install declaration files to be type safe.

Interfaces

When used in TypeScript the tabris module exports the following additional interfaces: