Confused about typechecking autocorrection

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?

youre overidding it with the type

local Items = {
	Test = {
		Price = 50,
		ItemName = "Hello world"
	}
}

this is all you need

I want the type to be there for autocorrecting when i make the items, it feels alot easier to make when i have to autocorrecting

[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
}
1 Like

I dident know you could typecheck individual items! This suites me the most, thanks!

1 Like

Learn more here: An introduction to Luau types | Luau

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.