1.2.0 • Published 7 days ago

factorio-types v1.2.0

Weekly downloads
8
License
MIT
Repository
github
Last release
7 days ago

factorio-types

Typescript declarations for the factorio mod API.

Installation

Install factorio-types from npm

Intended to be used with Typescript to Lua (which is a peer dependency)

Configuration

This library includes tsconfig.base.json in the root of the package with recommended configuration. This can be used as follows

tsconfig.json:

{
    "extends": "factorio-types/tsconfig.base.json",
    // other config like included files and output paths
}

Stages

Factorio mods and the api go through three distinct stages. Types are organized into namespaces runtime, prototype, and settings representing the types to be used in each stage. These types, and therefore their corresponding namespaces, are a compile-time-only feature as lua does not have types, however using a type from the wrong namespace during the wrong stage is likely to result in a runtime error as you will be attempting to access something Factorio does not let you access at that time.

Note there is some overlap between the types for the Settings and Prototype phases since both regularly provide prototype data to data.extend(), and in fact the settings-phase prototypes extend prototype.PrototypeBase. However, you should only be providing types from the settings namespace during the settings phase, and from the prototype namespace during the prototype phase.

Examples

A very minimal proof-of-concept showing basic toolchain setup can be found Here

A slightly more in-depth and realistic mode can be found Here

Lualib

Factorio makes various lua functions available to mods via LuaLib

Unlike the main parts of the factorio API, these do not exist as ambient globals but instead must be imported

import * as noise from "noise";
let x = noise.var_get('x');

Also unlike the main factorio API, these types are not documented in a machine-readable way and therefore are not generated here along with the main API types. Instead, they are hand-written as needed and as a result are incomplete and more likely to be wrong. See the existing declarations.

If there are types of lualib that you need but do not exist, either open an issue asking for it, or write the type definitions and open a pull request. If you would like to continue working with these types while they do not exist in factorio-types, you can add the import path to tstl.noResolvePaths in tsconfig.json, and then require() the paths, which will be typed as any.

tsconfig.json

{
    "tstl": {
        "noResolvePaths": ["math2d"]
    }
}

some-file.ts

const math2d = require('math2d');

Noise

The noise module has mostly-complete type definitions. A couple notes on usage due to the limitations of typescript

The noise.var function is named noise.var_get since var is a reserved word in typscript. It will be emitted as noise.var thanks to a @customName compiler annotation.

typescript

import * as noise from "noise";
let x = noise.var_get('x');

output lua

local noise = require("noise")
local x = noise.var("x")

Noise values have operator overloads for common arithmetic operations, however Typescript does not support operator overloading. These are implemented as Operator map types in the noise library, which will emit as regular operators in lua. Available operators are named following lua conventions for overloaded operators without the preceding double-underscore:

  • Addition: noise.add
  • Subraction: noise.sub
  • Multiplication: noise.mul
  • Division: noise.div
  • Unary Negation: noise.unm
  • Exponentiation: noise.pow

typescript

let x = noise.var_get('x');
let y = noise.var_get('y');
let sum = noise.add(x, y);

Output lua

local x = noise.var("x")
local y = noise.var("y")
local sum = x + y;
1.2.0

7 days ago

1.1.0

10 days ago

1.0.0

15 days ago

0.0.50

30 days ago

0.0.49

2 months ago

0.0.48

2 months ago

0.0.47

4 months ago

0.0.46

5 months ago

0.0.43

7 months ago

0.0.44

6 months ago

0.0.45

5 months ago

0.0.41

8 months ago

0.0.42

8 months ago

0.0.40

9 months ago

0.0.39

9 months ago

0.0.37

11 months ago

0.0.38

10 months ago

0.0.36

11 months ago

0.0.33

1 year ago

0.0.34

1 year ago

0.0.35

1 year ago

0.0.30

2 years ago

0.0.31

1 year ago

0.0.32

1 year ago

0.0.29

2 years ago

0.0.28

2 years ago

0.0.27

2 years ago

0.0.24

2 years ago

0.0.25

2 years ago

0.0.26

2 years ago

0.0.23

2 years ago

0.0.22

2 years ago

0.0.20

2 years ago

0.0.21

2 years ago

0.0.18

2 years ago

0.0.19

2 years ago

0.0.16

3 years ago

0.0.17

2 years ago

0.0.15

3 years ago

0.0.14

3 years ago

0.0.13

3 years ago

0.0.12

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago