typed-factorio 0.20.0-beta.1 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Changelog.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # v0.20.0
2
2
 
3
3
  - Updated to factorio version 1.1.56
4
- - Added custom-index-template.d.ts. You can use this if you would like to make a "custom" modification of the type definitions.
5
- - **This is an opt-in**: Some numeric types which represent indices/number,e.g. player_index, entity_number, are now branded numbers with their own type, e.g. `PlayerIndex` and `EntityNumber`. See the README for more info.
4
+ - This is an **opt-in** feature: Some numeric types which represent indices/number,e.g. player_index, entity_number, are now branded numbers with their own type, e.g. `PlayerIndex` and `EntityNumber`. See the README for more info.
5
+ - Added custom-index-template.d.ts to assist trying out custom changes to types in a project.
6
6
 
7
7
  # v0.19.0
8
8
 
package/README.md CHANGED
@@ -30,10 +30,7 @@ yarn add typed-factorio
30
30
 
31
31
  This will add the types for the runtime stage to your entire project.
32
32
 
33
- Notes:
34
- > When types are updated, or released for a new factorio version, you will need update your package version to get the types.
35
- >
36
- > There is an opt-in feature for stricter index types, see [Strict index types](#Strict index types) for more details.
33
+ Note: When types are updated, or released for a new factorio version, you will need update your package version to get the types.
37
34
 
38
35
  ### Settings and data stage
39
36
 
@@ -114,7 +111,6 @@ Table-or-array types will appear in the Table form when known to be in a read po
114
111
 
115
112
  For some concepts, there is also a special form for when the concept is used in a "read" position, where all table-or-array types are in Table form. These types are suffixed with `Read`, e.g. `ScriptPositionRead`.
116
113
 
117
-
118
114
  ### Types with subclasses
119
115
 
120
116
  Some classes have attributes that are documented to only work on particular subclasses. For these classes, e.g. `LuaEntity`, there are specific types that you can _optionally_ use:
@@ -134,11 +130,9 @@ This is done both to provide more accurate types, and for possible integration w
134
130
 
135
131
  ### Strict index types
136
132
 
137
- This feature is recommended, but currently is a **opt-in**. To opt in, use `typed-factorio/runtime/strict-index-types` instead of `typed-factorio/runtime` in your `tsconfig.json`.
138
-
139
- Some `uint` types which represent indices, e.g. player_index, entity_number, are now "branded" numbers with their own type, e.g. `PlayerIndex` and `EntityNumber`. These are assignable to `number`, but `number` is not assignable to them.
140
- These are indices that do not index into an array-like structure, and otherwise should usually not have arithmetic done to them. So, using a separate type helps ensure correctness.
141
- You can still use these types as keys in an index signature, e.g. `{ [index: PlayerIndex]: "foo" }`.
142
- You can cast "plain" numbers to these types, e.g. `2 as PlayerIndex`, only do this with some care.
133
+ This is a recommended **opt-in** feature. To opt in, add `"typed-factorio/strict-index-types"` to `compilerOptions > types` in your tsconfig.json (in addition to `"typed-factorio/runtime"`).
143
134
 
144
- - `player_index` and `surface_index` still allow the numeric constant 1 as a valid index, representing the first player or the default surface, respectively. This is allowed as this is a common enough use case.
135
+ Some `uint` types which represent indices, e.g. player_index, entity_number, can be "branded" numbers with their own type, e.g. `PlayerIndex` and `EntityNumber`. These are assignable to `number`, but a plain `number` is not directly assignable to them. This helps ensure correctness.
136
+ These are indices that do not index into an array-like structure, and otherwise should usually not have arithmetic done to them.
137
+ You can use these types as keys in an index signature, e.g. `{ [index: PlayerIndex]: "foo" }`.
138
+ You can cast "plain" numbers to these types, e.g. `1 as PlayerIndex`, do this with caution.
@@ -2,8 +2,6 @@
2
2
  // For example, if you want to modify a file, copy the file you want to modify to your project and make the changes. Then, copy this template file into your project and remove the line corresponding to modified file.
3
3
  // If you think your modification will be useful to others, please consider making a change suggestion by creating an issue on GitHub.
4
4
 
5
- // Replace "index-types.d.ts" with "index-types-strict.d.ts" below if you want strict index types.
6
-
7
5
  /// <reference types="lua-types/5.2" />
8
6
 
9
7
  // generated
@@ -2000,7 +2000,12 @@ interface LuaCustomTableMembers {
2000
2000
  */
2001
2001
  type LuaCustomTable<K extends string | number, V> = LuaCustomTableMembers &
2002
2002
  CustomTableIndexer<K, V> &
2003
- LuaPairsIterable<[number] extends [K] ? number : K, V>
2003
+ LuaPairsIterable<
2004
+ // this convoluted expression gives a number type if K includes a number, even if it includes a string, and K otherwise.
2005
+ // it also preserves number branding
2006
+ [number] extends [K extends number & IndexBrand<infer A> ? number : K] ? (K extends string ? never : K) : K,
2007
+ V
2008
+ >
2004
2009
 
2005
2010
  /**
2006
2011
  * Prototype of a damage.
@@ -3,36 +3,47 @@
3
3
  /** @noSelfInFile */
4
4
 
5
5
  /**
6
- * See {@link LuaPlayer#index LuaPlayer.index}..
6
+ * See {@link LuaPlayer#index LuaPlayer.index}.
7
7
  *
8
- * This can be a "branded" type for stricter types; see docs on how to opt-in to this.
8
+ * If using strict-index-types, and you need to use a plain number for this type, you can use a cast, e.g. `1 as PlayerIndex`.
9
9
  */
10
- type PlayerIndex = uint
10
+ type PlayerIndex = uint & IndexBrand<"_playerIndexBrand">
11
11
 
12
12
  /**
13
- * See {@link LuaSurface#index LuaSurface.index}..
13
+ * See {@link LuaSurface#index LuaSurface.index}.
14
14
  *
15
- * This can be a "branded" type for stricter types; see docs on how to opt-in to this.
15
+ * If using strict-index-types, and you need to use a plain number for this type, you can use a cast, e.g. `1 as SurfaceIndex`.
16
16
  */
17
- type SurfaceIndex = uint
17
+ type SurfaceIndex = uint & IndexBrand<"_surfaceIndexBrand">
18
18
 
19
19
  /**
20
- * See {@link LuaEntity#unit_number LuaEntity.unit_number}..
20
+ * See {@link LuaEntity#unit_number LuaEntity.unit_number}.
21
21
  *
22
- * This can be a "branded" type for stricter types; see docs on how to opt-in to this.
22
+ * If using strict-index-types, and you need to use a plain number for this type, you can use a cast, e.g. `1 as UnitNumber`.
23
23
  */
24
- type UnitNumber = uint
24
+ type UnitNumber = uint & IndexBrand<"_unitNumberBrand">
25
25
 
26
26
  /**
27
- * See {@link LuaGuiElement#index LuaGuiElement.index}..
27
+ * See {@link LuaGuiElement#index LuaGuiElement.index}.
28
28
  *
29
- * This can be a "branded" type for stricter types; see docs on how to opt-in to this.
29
+ * If using strict-index-types, and you need to use a plain number for this type, you can use a cast, e.g. `1 as GuiElementIndex`.
30
30
  */
31
- type GuiElementIndex = uint
31
+ type GuiElementIndex = uint & IndexBrand<"_guiElementIndexBrand">
32
32
 
33
33
  /**
34
- * See {@link LuaBootstrap#register_on_entity_destroyed LuaBootstrap.register_on_entity_destroyed}..
34
+ * See {@link LuaBootstrap#register_on_entity_destroyed LuaBootstrap.register_on_entity_destroyed}.
35
35
  *
36
- * This can be a "branded" type for stricter types; see docs on how to opt-in to this.
36
+ * If using strict-index-types, and you need to use a plain number for this type, you can use a cast, e.g. `1 as RegistrationNumber`.
37
37
  */
38
- type RegistrationNumber = uint64
38
+ type RegistrationNumber = uint64 & IndexBrand<"_registrationNumberBrand">
39
+
40
+ interface __OptInFeatures {}
41
+
42
+ /**
43
+ * Equals a branded type when __OptInFeatures contains strictIndexTypes, otherwise equals `unknown`.
44
+ */
45
+ type IndexBrand<B extends string> = "strictIndexTypes" extends keyof __OptInFeatures
46
+ ? {
47
+ [K in B]: any
48
+ }
49
+ : unknown
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typed-factorio",
3
- "version": "0.20.0-beta.1",
3
+ "version": "0.20.0",
4
4
  "description": "Featureful typescript definitions for the the Factorio modding lua api.",
5
5
  "keywords": [
6
6
  "factorio",
@@ -1,15 +1,10 @@
1
1
  /** @noSelfInFile */
2
2
  ///<reference types="lua-types/5.2" />
3
3
 
4
- /**
5
- * Iterating with `pairs` will only iterate the array part of a {@link LuaCustomTable}.
6
- *
7
- * **Note**: you can also iterate on a LuaCustomTable directly without using `pairs`, e.g. `for(const [k, v] of table)`.
8
- */
9
- declare function pairs<V>(table: LuaCustomTable<number | string, V>): LuaIterable<LuaMultiReturn<[number, V]>>
10
-
11
4
  /** **Note**: you can also iterate on a LuaCustomTable directly without using `pairs`, e.g. `for(const [k, v] of table)`. */
12
- declare function pairs<K extends number | string, V>(table: LuaCustomTable<K, V>): LuaIterable<LuaMultiReturn<[K, V]>>
5
+ declare function pairs<T extends LuaCustomTable<any, any>>(
6
+ table: T
7
+ ): LuaIterable<LuaMultiReturn<T extends Iterable<infer E> ? E : never>>
13
8
 
14
9
  /** @deprecated {@link LuaCustomTable} cannot be iterated with `ipairs`; Use {@link pairs} instead. */
15
10
  declare function ipairs(table: LuaCustomTable<any, any>): never
@@ -0,0 +1,3 @@
1
+ interface __OptInFeatures {
2
+ strictIndexTypes: true
3
+ }
@@ -1,52 +0,0 @@
1
- // This is an auto-generated file. Do not edit directly!
2
-
3
- /** @noSelfInFile */
4
-
5
- /**
6
- * See {@link LuaPlayer#index LuaPlayer.index}.
7
- *
8
- * If you need to use a number/numeric literal for this type, you can use a cast, e.g. `2 as PlayerIndex`.
9
- */
10
- type PlayerIndex =
11
- | (uint & {
12
- _playerIndexBrand: void
13
- })
14
- | 1
15
-
16
- /**
17
- * See {@link LuaSurface#index LuaSurface.index}.
18
- *
19
- * If you need to use a number/numeric literal for this type, you can use a cast, e.g. `2 as SurfaceIndex`.
20
- */
21
- type SurfaceIndex =
22
- | (uint & {
23
- _surfaceIndexBrand: void
24
- })
25
- | 1
26
-
27
- /**
28
- * See {@link LuaEntity#unit_number LuaEntity.unit_number}.
29
- *
30
- * If you need to use a number/numeric literal for this type, you can use a cast, e.g. `2 as UnitNumber`.
31
- */
32
- type UnitNumber = uint & {
33
- _unitNumberBrand: void
34
- }
35
-
36
- /**
37
- * See {@link LuaGuiElement#index LuaGuiElement.index}.
38
- *
39
- * If you need to use a number/numeric literal for this type, you can use a cast, e.g. `2 as GuiElementIndex`.
40
- */
41
- type GuiElementIndex = uint & {
42
- _guiElementIndexBrand: void
43
- }
44
-
45
- /**
46
- * See {@link LuaBootstrap#register_on_entity_destroyed LuaBootstrap.register_on_entity_destroyed}.
47
- *
48
- * If you need to use a number/numeric literal for this type, you can use a cast, e.g. `2 as RegistrationNumber`.
49
- */
50
- type RegistrationNumber = uint64 & {
51
- _registrationNumberBrand: void
52
- }
@@ -1,18 +0,0 @@
1
- /// <reference types="lua-types/5.2" />
2
-
3
- // generated
4
- /// <reference path="../generated/builtin-types.d.ts" />
5
- /// <reference path="../generated/global-objects.d.ts" />
6
- /// <reference path="../generated/defines.d.ts" />
7
- /// <reference path="../generated/events.d.ts" />
8
- /// <reference path="../generated/classes.d.ts" />
9
- /// <reference path="../generated/concepts.d.ts" />
10
- /// <reference path="../generated/index-types-strict.d.ts" />
11
-
12
- // other runtime
13
- /// <reference path="librariesAndFunctions.d.ts" />
14
- /// <reference path="pairs.d.ts" />
15
-
16
- // lualib
17
- /// <reference path="util.d.ts" />
18
- /// <reference path="mod-gui.d.ts" />