type Settings = {
["Audio"]: {
MainVolume: number
},
}
function doThing(settingTable: Settings)
for category: string, list: any in settingTable do
end
end
Why does this warning pop up?
the category in the type is defined in double quotes, so why can it not be converted into a string here? Changing : string to : any or : unknown works.
Apparently, having another type for just the audio key does fix the type checking issue even with --!strict on, but I’d rather not recommend it unless that’s the only feasible solution.
type audio = "Audio"
type Settings = {
[audio]: {
MainVolume: number
}
}
function doThing(settingTable: Settings)
for category: string, list: any in settingTable do
end
end
Why are you iterating through settings like that anyway? If you define table keys with literal values, that implies that you are not going to iterate through the table. This is intended and expected behavior.
I just have the settings sorted into categories in the actual data table.
The type is structured the same way.
I only have to loop through the settings like once in the entire game though.
This is how I thought it works, and it does work for autocomplete, but it causes a warning for this.
So is there any way to fix it, or do I just use pairs instead of generalized iteration?
EDIT: Seems like typecasting this also works:
function doThing(settingTable: Settings)
for category: string, list: any in settingTable :: {[string]: any} do
end
end
The point of type checking is to notify the intellisense that something is some value at a certain point. If you define table literals as keys with the keys having different layouts from each other, intellisense has no idea what the key and what value it will be at a certain time (hence “unknown”). This is why you need a general indexer for any of this to work.
If you want to iterate through a dictionary with a dictionary, your type would be: