Explain, is this is possible?

okay, I have the simplest possible code in which I tried to figure out how to work with generics. Look:

--!strict
local function addInfo<T>(info: { IDK: T }): T
	error()
end

this type of input can only accept something like
addInfo { IDK = tonumber("I SWEAR I'M A NUMBER")}and the result its a number type.

however, let’s be honest, who is interested in the addInfo function, which accepts only one single key in the table or accepts [“string”], which makes my question completely meaningless
I tried to do something like this:

local function addInfo<K,T>(info: { [K]: T }): {K}
	error()
end

addInfo { ["value"] = 1 }
I expect: well, yes, the key is a string, I should get a table with rows at the output… No, it’s not, roblox think its {Unknown}, I ~understand why.
However, this does not solve my question.

--!strict

type smg<T> = {
	name: string,
	value: T
}

type myGenericType<T> = {
	name: string,
	smthing: { [[KEYS THAT WAS PASSED IN KEY OF THE TABLE!!!]]: smg<T> },
}

local function addInfo<T>(info: { key: T }): myGenericType<T>
	error()
end

local a = addInfo {
	lolKey = 1
}

What do I want to do?
I want to be able to write something like a.lolKey.value and get hints in real time.
where roblox will determine by itself that value is a number and will offer hints to lolKey

1 Like

Im forgot to say, please

This text will be blurred

Its not possible because the content of addInfo cannot be proved for sure at a given place where you inspect it. It would be possible if Lua had something like a struct, but tables can have their elements and types of those elements changed whenever.

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