Skip to content

table properties using builtin names are incorrectly marked as unsafe and renamed with underscores #833

@chrisbajorin

Description

@chrisbajorin

Using identifiers in the builtins list as property names is valid Lua:

local Vector = {}
Vector.__index = Vector
function Vector.new(x,y)
    return setmetatable({
        x = x,
        y = y
    }, Vector)
end

function Vector:unpack()
    return self.x, self.y
end

local v = Vector.new(2,3)
local x,y = v:unpack()
print(x,y)

If I were to write a declaration for this:

interface Vector {
    new: (this: void, x: number, y: number) => Vector;
    /** @tupleReturn **/
    unpack(): [number, number];
}
let vec2 = {} as Vector;

let v = vec2.new(2,3);
let [x,y] = v.unpack();
let [a,b] = v["unpack"]();

which compiles to:

vec2 = {}
v = vec2.new(2, 3)
x, y = v:____unpack()
a, b = v.unpack(v)

___unpack() will cause an error. Accessing it via string index is currently a workaround.

I believe this is related to #789

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions