Skip to content

Support for..of on LuaTable #1023

@tomblind

Description

@tomblind

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions