Skip to content

Latest commit

 

History

History

tstl-types

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

tfm-tstl-types

Provides the TypeScriptToLua environment definition for Transformice.

Install

  1. Get this package from npm
npm install -D tfm-tstl-types
# or
yarn add -D tfm-tstl-types
  1. Modify your tsconfig.json
{
  "compilerOptions": {
+    "types": ["tfm-tstl-types/types"]
  }
}

Usage

Most of the usage and function calls remain largely similar to that of Lua.

Defining events

eventLoop = (elapsedTime, remainingTime) => {
  // Intellisense will pick up the types.
  // You can hover over the `eventLoop` for additional context.
}

// Or via `globalThis` which translates to `_G`.
globalThis.eventNewPlayer = function(playerName) {
  // `playerName` is of type `string`
}

Enums

Enums can be used the same way as in Lua. These are just normal namespaced consts.

const id = tfm.enum.bonus.electricArc

The type library also provides const enums for use as types or inlining at compile-time.

const id = tfm.enum.bonus.electricArc as tfm.Enums.BonusType
// ^ id is of type tfm.enum.BonusType

Sometimes, you may find that you need to use IDs that don't exist within the const enums.

eventContactListener = (playerName, groundId, contactInfos) => {
  if (groundId == 42069) {
      // ^ error: "This condition will always return 'false' since the types 'GroundType' and
      //           '42069' have no overlap. ts(2367)"
  }
}

In this case, it is reasonable to just cast the variable back into its primitive type.

eventContactListener = (playerName, groundId: number /* cast at source */, contactInfos) => {
    // Or cast at assertion
    if (groundId as number == 42069) {
    }
}