How do I typecheck an array that could have any number of strings? (w/in a function)

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

HELPPPPP

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?

3 Likes

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 }
2 Likes

sorry, let me simplify
i want a type that is an array* that could contain any amount of strings, but only strings.

So in that case, type thing = {string} should work for you

1 Like

i have same problem…
Have you found any solutions?
It just HATES mixed tables for no reason…

Not sure I get what you’re doing here..

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)

output:
3
1 a
2 b
3 c