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: docs/advanced/language-extensions.md
+31-10
Original file line number
Diff line number
Diff line change
@@ -230,9 +230,25 @@ You can also map functions to table accessors (`__index` and `__newindex`). See
230
230
231
231
## Lua Table Types
232
232
233
+
The `LuaTable` type is provided to allow direct creation and manipulation of Lua tables. This is useful if you want to use a table that uses types other than string for its keys, as that is not supported by Typescript. Calls to lua method tables are translated to simple lua:
234
+
235
+
-`table.get(key)` Get a value by key -> `table[key]`
236
+
-`table.set(key, value)` Set a value for key -> `table[key] = value`
237
+
-`table.has(key)` Check if key is in table -> `table[key] ~= nil`
238
+
-`table.delete(key)` Remove key (and its value) from table -> `table[key] = nil`
239
+
240
+
### Generic key and value types
241
+
242
+
`LuaTable` can be used without explicitly providing types for the keys and values, but also allows you to specify the type of keys and values in the table:
const untypedLuaTable =newLuaTable(); // Same as LuaTable<AnyNotNil, any>
247
+
```
248
+
233
249
### Getting and Setting
234
250
235
-
The `LuaTable` type is provided to allow direct creation and manipulation of Lua tables. This is useful if you want to use a table that uses types other than string for its keys, as that is not supported by Typescript. Calls to `get` and `set` on the table will transpile directly to `value = table[key]` and `table[key] = value`.
251
+
Calls to `get` and `set` on the table will transpile directly to `value = table[key]` and `table[key] = value`.
236
252
237
253
Example:
238
254
@@ -273,7 +289,7 @@ print(#tbl)
273
289
To iterate over a `LuaTable`, use `pairs()`. (This requires the `lua-types` library to be installed.)
274
290
275
291
```ts
276
-
const tbl =newLuaTable();
292
+
const tbl =newLuaTable<number, string>();
277
293
278
294
tbl.set(3, "bar");
279
295
tbl.set(4, "bar");
@@ -287,14 +303,6 @@ for (const [key, value] of pairs(tbl)) {
287
303
288
304
(Remember that in Lua, `pairs()` returns the keys in a random order.)
289
305
290
-
### Restricting the Types
291
-
292
-
`LuaTable` can also be restricted to use only certain types as keys and values:
293
-
294
-
```ts
295
-
const tbl =newLuaTable<KeyType, ValueType>();
296
-
```
297
-
298
306
### Custom Getters and Setters
299
307
300
308
If you have a type that uses non-string keys, you can use `LuaTableGet` and `LuaTableSet` function types to declare your own getters & setters, similar to [Operator Map Types](#operator-map-types).
There are more LuaTable functions other than `LuaTableGet` and `LuaTableSet` that you can use:
361
+
362
+
-`LuaTableGet` - Standalone function that gets a value by key from a table.
363
+
-`LuaTableGetMethod` - Method that gets a value by key from the table containing this method.
364
+
-`LuaTableSet` - Standlone function that sets a value to a key in a table.
365
+
-`LuaTableSetMethod` - Method that sets a value to a key in the table containing this method.
366
+
-`LuaTableHas` - Standalone function that checks if a key is present in a table.
367
+
-`LuaTableHasMethod` - Method that checks if a key is present in the table containing this method.
368
+
-`LuaTableDelete` - Standalone function that removes a key and its value from a table.
369
+
-`LuaTableDeleteMethod` - Method that removes a key and its value from table containing this method.
370
+
350
371
## $vararg Constant
351
372
352
373
Lua allows use of the ellipsis operator (`...`) to access command line arguments passed when executing a script file. To access this from Typescript, you can use the `$vararg` constant in a spread expression.
0 commit comments