X Tutup
Skip to content

Spread operator #121

@dmarcuse

Description

@dmarcuse

It would be nice to be able to use the spread operator. For arrays, it's pretty straightforward:

const example = [ "a", ...[ "b", "c" ] ];
print(...example);

can be compiled to

local example = { "a", table.unpack("b", "c" }
print(table.unpack(example))

Objects, on the other hand, can't easily be implemented in a way that emulates their behavior in JS - the easiest way to implement it might be to add a function to typescript_lualib, so that something like this could be done:

const example = { a: 1, ...{ a: 2, b: 3 } }

...might be compiled to...

-- typescript_lualib.lua
function TS_spreadobj(a, b)
	local c = {}

	for k,v in pairs(a) do c[k] = v end
	for k,v in pairs(b) do c[k] = v end

	return c
end

-- somewhere else
local example = TS_spreadobj({a = 1}, {a = 2, b = 3})

This introduces a bit of extra complexity - we'd need to separate the literal keys in the object from the spread operator and transpile them to separate expressions, and finally wrap it all in a call.

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

      X Tutup