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
You can also map functions to table accessors (`__index` and `__newindex`). See [Lua Table Types](#lua-table-types).
229
+
:::
230
+
231
+
## Lua Table Types
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 `get` and `set` on the table will transpile directly to `value = table[key]` and `table[key] = value`.
234
+
235
+
Example:
236
+
237
+
<SideBySide>
238
+
239
+
```ts
240
+
const tbl =newLuaTable();
241
+
242
+
tbl.set("foo", "bar");
243
+
console.log(tbl.get("foo"));
244
+
245
+
const objectKey = {};
246
+
tbl.set(objectKey, "baz");
247
+
console.log(tbl.get(objectKey));
248
+
249
+
tbl.set(1, "bah");
250
+
console.log(tbl.length());
251
+
```
252
+
253
+
```lua
254
+
tbl= {}
255
+
256
+
tbl.foo="bar"
257
+
print(tbl.foo)
258
+
259
+
objectKey= {}
260
+
tbl[objectKey] ="baz"
261
+
print(tbl[objectKey])
262
+
263
+
tbl[1] ="bah"
264
+
print(#tbl)
265
+
```
266
+
267
+
</SideBySide>
268
+
269
+
`LuaTable` can also be restricted to use only certain types as keys and values:
270
+
271
+
```ts
272
+
const tbl =newLuaTable<KeyType, ValueType>();
273
+
```
274
+
275
+
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).
276
+
277
+
Example:
278
+
279
+
<SideBySide>
280
+
281
+
```ts
282
+
interfaceId {
283
+
idStr:string;
284
+
}
285
+
286
+
interfaceIdDictionary {
287
+
get:LuaTableGetMethod<Id, string>;
288
+
set:LuaTableSetMethod<Id, string>;
289
+
}
290
+
291
+
declareconst dict:IdDictionary;
292
+
const id:Id= { idStr: "foo" };
293
+
dict.set(id, "bar");
294
+
console.log(dict.get(id));
295
+
```
296
+
297
+
```lua
298
+
id= {idStr="foo"}
299
+
dict[id] ="bar"
300
+
print(dict[id])
301
+
```
302
+
303
+
</SideBySide>
304
+
305
+
That example uses the `Method` versions of `LuaTableGet` and `LuaTableSet`. There are also stand-alone versions.
0 commit comments