Skip to content

Commit aef9fc9

Browse files
committed
Add docs for LuaMap and LuaSet
1 parent 0605921 commit aef9fc9

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

docs/advanced/language-extensions.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,27 @@ end
171171

172172
</SideBySide>
173173

174+
Or, if you only care about the key values, you can use `LuaPairsKeyIterable`:
175+
176+
<SideBySide>
177+
178+
```ts
179+
interface MyType extends LuaPairsKeyIterable<number> {}
180+
declare const obj: MyType;
181+
182+
for (const key of obj) {
183+
console.log(key);
184+
}
185+
```
186+
187+
```lua
188+
for key in pairs(obj) do
189+
print(key)
190+
end
191+
```
192+
193+
</SideBySide>
194+
174195
## Operator Map Types
175196

176197
Lua supports overloading operators on types using [metatable methods](https://www.lua.org/manual/5.4/manual.html#2.4) such as `__add`. But, JavaScript and TypeScript do not support this. In order to use overloaded operators on types that support them, you can declare special mapping functions in TS that will translate to those operators in Lua.
@@ -343,6 +364,14 @@ end
343364

344365
(Remember that in Lua, `pairs()` returns the keys in a random order.)
345366

367+
### `LuaMap` and `LuaSet`
368+
369+
Similar to `LuaTable`, the `LuaMap` and `LuaSet` types are provided to represent Lua tables used as a map or set. These are much more performant than the `Set`/`Map` classes, but do not come with all the same features (such as guaranteed insertion order).
370+
371+
- `LuaMap` has the same methods as `LuaTable`, except that `table.get(key)` may also return `undefined`.
372+
- `LuaSet`, instead of `table.get/table.set`, has `table.add(value)`, which translates to `table[value] = true`.
373+
- There are also the readonly variants `ReadonlyLuaMap` and `ReadonlyLuaSet`.
374+
346375
### Custom Getters and Setters
347376

348377
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).
@@ -395,9 +424,9 @@ IdDictionary.set(dict, id, "bar");
395424
console.log(IdDictionary.get(dict, id));
396425
```
397426

398-
### All custom LuaTable functions
427+
### All custom Lua table functions
399428

400-
There are more LuaTable functions other than `LuaTableGet` and `LuaTableSet` that you can use:
429+
There are more Lua table functions other than `LuaTableGet` and `LuaTableSet` that you can use:
401430

402431
- `LuaTableGet` - Standalone function that gets a value by key from a table.
403432
- `LuaTableGetMethod` - Method that gets a value by key from the table containing this method.
@@ -407,6 +436,8 @@ There are more LuaTable functions other than `LuaTableGet` and `LuaTableSet` tha
407436
- `LuaTableHasMethod` - Method that checks if a key is present in the table containing this method.
408437
- `LuaTableDelete` - Standalone function that removes a key and its value from a table.
409438
- `LuaTableDeleteMethod` - Method that removes a key and its value from table containing this method.
439+
- `LuaTableAddKey` - Standalone function that sets a given key to `true`
440+
- `LuaTableAddKeyMethod` - Method that sets a given key to `true`
410441

411442
## $vararg Constant
412443

0 commit comments

Comments
 (0)