Allow for..of loops to iterate LuaTable typed objects by transpiling to using pairs.
Possible implementations:
Use Symbol.iterator on LuaTable
interface LuaTable<TKey extends {} = {}, TValue = any> {
[Symbol.iterator](): IterableIterator<LuaMultiReturn<[TKey, TValue]>>
}
declare const t: LuaTable<string, number>;
for (const [k, v] of t) { // for k, v in pairs(t)
}
pros: simple
cons: restricts functionality to LuaTable type only
Create dedicated extension type and extend LuaTable:
declare type LuaPairsIterable<TKey, TValue> = Iterable<[TKey, TValue]> & LuaExtension<"__luaPairsIterableBrand">;
declare interface LuaTable<TKey extends {} = {}, TValue = any> extends LuaPairsIterable<TKey, TValue> {
length: LuaLengthMethod<number>;
get: LuaTableGetMethod<TKey, TValue>;
set: LuaTableSetMethod<TKey, TValue>;
}
declare const t: LuaTable<string, number>;
for (const [k, v] of t) { // for k, v in pairs(t)
}
pros: allows functionality to be applied to any types that may support pairs iteration
cons: ugly use of extends on LuaTable
Allow
for..ofloops to iterate LuaTable typed objects by transpiling to usingpairs.Possible implementations:
Use
Symbol.iteratoronLuaTablepros: simple
cons: restricts functionality to
LuaTabletype onlyCreate dedicated extension type and extend
LuaTable:pros: allows functionality to be applied to any types that may support
pairsiterationcons: ugly use of extends on
LuaTable