You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+33-27
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,8 @@ The generator uses both the Factorio api docs JSON and manually defined addition
10
10
To use in your [TypescriptToLua](https://typescripttolua.github.io/) project:
11
11
12
12
1. Install this package: `npm install typed-factorio`
13
-
- Note: When types are updated for a new factorio version, you will need to update this package.
13
+
14
+
- Note: When types are updated for a new factorio version, you will need to update this package.
14
15
15
16
2. Add types for the [stages](https://lua-api.factorio.com/1.1.89/index.html) used to `tsconfig.json > compilerOptions > types`.
16
17
The available stages are `"typed-factorio/settings"`, `"typed-factorio/prototype"`, and `"typed-factorio/runtime"`.
@@ -35,7 +36,8 @@ Here are some notes on using the types:
35
36
36
37
### Types for other stages
37
38
38
-
No matter which stage(s) are selected, _type_ definitions for all stages are available in the modules `"factorio:settings"`, `"factorio:prototype"`, and `"factorio:runtime"`:
39
+
No matter which stage(s) are selected, _type_ definitions for all stages are available in the modules `"factorio:settings"`, `"factorio:prototype"`, and `"factorio:runtime"`:
@@ -45,40 +47,46 @@ import { LuaEntity } from "factorio:runtime"
45
47
You can also include just `"typed-factorio"` in your `types` field. This will include only global variables available to _all_ stages.
46
48
47
49
### `data.extend()` types
48
-
In the settings and prototype stages, the `data` global variable is available.
50
+
51
+
In the settings and prototype stages, the `data` global variable is available.
49
52
50
53
For [performance reasons](https://github.com/microsoft/TypeScript/wiki/Performance#preferring-base-types-over-unions), `data.extend()` is by default loosely typed.
51
54
To get full type checking, you can use specific types in one of the following ways:
55
+
52
56
```ts
53
57
// Use `satisfies` to check types:
54
58
data.extend([
55
-
{
56
-
type: "ammo-category",
57
-
name: "foo",
58
-
} satisfiesAmmoCategory,
59
-
{
60
-
type: "item",
61
-
name: "bar",
62
-
// ...other fields
63
-
} satisfiesItemPrototype,
59
+
{
60
+
type: "ammo-category",
61
+
name: "foo",
62
+
} satisfiesAmmoCategory,
63
+
{
64
+
type: "item",
65
+
name: "bar",
66
+
// ...other fields
67
+
} satisfiesItemPrototype,
64
68
])
65
69
66
70
// List types as a type argument to `extend`:
67
71
data.extend<AmmoCategory|ItemPrototype>([
68
-
{
69
-
type: "ammo-category",
70
-
name: "foo"
71
-
},
72
-
{
73
-
type: "item",
74
-
name: "bar",
75
-
// ...other fields
76
-
}
72
+
{
73
+
type: "ammo-category",
74
+
name: "foo",
75
+
},
76
+
{
77
+
type: "item",
78
+
name: "bar",
79
+
// ...other fields
80
+
},
77
81
])
78
82
79
83
// Use types on separate variables:
80
-
const fooCategory:AmmoCategory= {/* ... */}
81
-
const barItem:ItemPrototype= {/* ... */}
84
+
const fooCategory:AmmoCategory= {
85
+
/* ... */
86
+
}
87
+
const barItem:ItemPrototype= {
88
+
/* ... */
89
+
}
82
90
data.extend([fooCategory, barItem])
83
91
```
84
92
@@ -149,6 +157,7 @@ Event IDs (`defines.events`) hold type info for their corresponding event type a
149
157
You can pass an event data type parameter to `script.generate_event_name<T>()`, and it will return a `CustomEventId` that includes type info.
150
158
151
159
### Optional custominput name checking
160
+
152
161
You can optionally enable type-checking for custom input names (for `script.on_event` and `CustomInputPrototype`).
153
162
To do so, specify names by extending the CustomInputNames interface like so:
If not specified, `CustomInputName` defaults to just `string`.
@@ -201,11 +210,8 @@ Similarly, `GuiSpec` (the table passed to `LuaGuiElement.add`), is also a discri
201
210
202
211
This is done both to provide more accurate types, and for possible integration with [JSX](https://typescripttolua.github.io/docs/jsx/).
203
212
204
-
205
213
## Support
206
214
207
215
If you find this project useful, consider tipping me on Kofi!
208
216
209
217
<ahref='https://ko-fi.com/Z8Z1VI6P8'target='_blank'><imgheight='36'style='border:0px;height:36px;'src='https://storage.ko-fi.com/cdn/kofi2.png?v=3'border='0'alt='Buy Me a Coffee at ko-fi.com' /></a>
0 commit comments