Defold API
Getting accurate types for the Defold API is an important part of ts-defold. Here are some ways to get the definitions you need:
✨ The types library is built-in into all ts-defold templates, and is automatically
published to keep up with the latest changes to Defold.
💥 Defold Annotations for Typescript Is an alternative third-party tool for generating types based on the Defold API and Lua features.
Working with Messages
The Defold engine is built around communication using messages, so it helps to have accurate definitions of the built-in messages.
💥 Defold Annotations for Typescript can generate a definitions file with Defold's built-in messages.
Working with Vector Math
TypeScript doesn't have a built-in way to understand the resulting type of
an operation involving vectors. It incorrectly assumes the product of
any mathematical operation has the type number.
const result = go.get_position() + vmath.vector3(1, 1, 1); // type: number ?!??
If you're confident about the result of an operation, you can override the type:
const result = (go.get_position() + vmath.vector3(1, 1, 1)) as vmath.vector3; // type: vmath.vector3
Or, to enable proper type checking, you can use the
Operator Map Types included in ts-defold's built-in types library.
Each Vector3 and Vector4 object has the methods add, sub, mul, div, and unm (negation).
Matrixes and Quaternions have the mul (multiplication) method.
const result = go.get_position().add(vmath.vector3(1, 1, 1)) // type: vmath.vector3