I’m trying to typecheck an array that could have any number of strings within a function.
I can’t figure it out, though. I tried using any as an indexer and typechecked it as a string, but it said “Unexpected array-like table item: the indexer key type of this table is not number.”
Since this is a very specific topic, obviously I can’t find any solutions. However it has to be this specific because I know there are unique syntaxes for both having a varying-length table and typechecking arrays.
type thing = {
[any]: string
}
function idk(something: thing)
end
i dont know if i understand this 100%, but couldnt you just do type thing = {string} so that luau would know that it’s a table potentially full of strings?
Currently your type thing is defining a dictionary where the keys have type any (any type) and the values are string. To define an array (a table containing only number keys), you can do what @MercuryCursed said:
type ArrayOfStrings = {string}
--Don't use the type below, but the type above is essentially equivalent to:
type VerboseArrayOfStrings = { [number]: string }
type Thing = {string}
function idk(something: Thing)
print(#something)
for i, str in ipairs(something) do
print(i, str)
end
end
local example: Thing = {"a", "b", "c"}
idk(example)