So basically I have this Luau type checking warning and it’s driving me crazy
Code
--!strict
type Cost = {string, number} -- "a: Unknown global 'number'"
export type ItemClass = {
Type: number,
Level: number,
IconId: number,
Cost: Cost
}
local item: ItemClass = {
Type = 1,
Level = 1,
IconId = 123,
Cost = {"Gold", 100}
}
local function a(b: ItemClass)
(b.Cost[1]):format()
end
Cost
should only ever be an array, with the first index a string, and the second index a number.
If I use a union instead and I try to run function a
, it would error because Cost
under b
(type is ItemClass) is string | number
, meaning it will warn me that it could be a number, and that I can’t run format
on a number. The thing is, Cost[1] will always be a string, and Cost[2] will always be a number. There will never be more than 2 indexes, and it must be a regular array.
Only resource I can find that relates to this is here, which basically tells me it might not be possible yet (can’t use number literals for the index):
https://devforum.roblox.com/t/luau-type-checking-release/878947/78
Is this possible at the moment? Or will I just have to use a union, and find some way to silence the warning if I get one?