Im trying to make autocorrect for when i make a system in my game, which contains data.
Currently, the typechecking is acting like each dictionary item does not exist.
For this example, lets assume we are using shop items.
local Items:ShopItems = {
Test={
Price=50,
ShopName="Hello world"
}
}
And the ShopItems type is this:
export type ShopItems = {
[string]:{
Price:number,
ItemName:string
}
}
With this snippet , whenever i type Items.T, it does not show Test as a option.
However, If i type ANYTHING into Items, such as Items.Hello or Items.World, It does autocorrect Price and ItemName.
Ive tried adding typeof(Items), but it still wont autocorrect. Im not to familiear with OOP, so I dont know what other solutions i could have tried.
How do I get the dictionary items to typecheck without manually defining them?
[string] means any string index, so it can’t know that ‘Test’ is necessarily one of those indexes. It can’t autocomplete an index it doesn’t know about! All it knows is that any string will be {Price: number, ItemName: string}. If you want Test to autocomplete, you need to define it.
type ShopItem = {
Price: number,
ItemName: string,
}
export type ShopItems = {
Test: ShopItem,
}
local Items: ShopItems = {
Test = {
Price= 50,
ShopName= "Hello world"
}
}
Or, alternatively, you can explicitly type each item!
type ShopItem = {
Price: number,
ItemName: string,
}
local Items = {
Test = {
Price= 50,
ShopName= "Hello world"
} :: ShopItem
}