New Type Solver keyof, index

Hey guys, using the new solver I try to create this

type Person = {
	name: string,
	job: string,
	age: number
}

type key = keyof<Person>
type value = index<Person, key>

local john:  { [key]: value } = {
	name = "John",
	job = "Developer",
	age = 15
}

local johnAge = john['age']

I know i can do local john: Person = { ... } but this is just for experiment, does anyone know why when I hover, I get any, instead of correct type like number | string, and also why for autocomplete I need john[‘age’] instead of john.age?

1 Like

This is one of the weird side effects with the current way string literals are inferred.

Basically, the string "age" in john["age"] is being inferred as a string type instead of a string singleton type, which doesn’t match with your expanded type for john which is {["name" | "job" | "age"]: string | number}. There’s code to explicitly infer this case as any, if I recall correctly.

For this to work, the inference rules need to change, and I think the Luau team is hesitant on doing that because it literally is a code breaking change now thanks to type functions.

1 Like