Is this possible? (typechecking)

local foo = {
    bar = {
        baz = 1
    }
} 

function qux(foobar) : typeof(foo[foobar])
    -- ....
end 

qux("bar")

How could I make this autocomplete?

For example (this is photoshopped)

(Don’t mind the other code I’ve run I did stupid stuff)

This is all a little confusing. What exactly are you trying to achieve here?

Like if I input a string as the first argument of the “qux” function it will autocomplete as if it is foo[string]

I don’t think what you want to do is exactly possible, but I think creating an enum table would work just the same.

local foo = {
    baz = 1
}
qux(foo.baz)

This would show the name of the selected enum item and convert it to the value you want. Let me know if your solution can’t use this type of system.

AFAIK, you can’t access the function parameters in the typechecking there. I could be wrong though.

2 Likes

This is pretty different from what you want but it’s the closest I can get to something that looks somewhat okay?

local foo = {
	bar = {
		baz = 1
	}
} 

function qux<T>(foobar: string): T
	-- might need to cast to 'any' here
	return foo[foobar]
end 

local value: typeof(foo.bar) = qux("bar")

-- or without assigning to variable
(qux("bar") :: typeof(foo.bar)).baz = 1

In any case, I think the only way to get autocomplete working would be to explicitly cast to the type you want after you call the function like I did above.

1 Like