How to remove an element from a table?

How to remove an element from a Lua table (not array, which for some reason some people also call it a table…). Setting value to nil should work, but then why:

local a = {}
a["a"] = 1
a["a"] = nil -- Type Error: Type 'nil' could not be converted into 'number'

This happens for both old type solver and a new one.

rawset(a, "a", nil)

This works, but doesn’t seem like an intended way of doing it.

are you using strict mode?

that is the intended way to do it you are right

if you are using strict mode you can do that to remove the error

--!strict
local a :{ [string] : number?} = {}
a["a"] = 1
a["a"] = nil -- Type Error: Type 'nil' could not be converted into 'number'

number? means that it can be either number or nil

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.