Luau keyof operator

As a Roblox developer, it is currently too hard to
convert table/type keys to a type. My idea is to add a keyof operator that would turn the keys into type.

Example:

--!strict

local a = { 
   a = 1
   b = 2
}

function GetValue(Key: keyof(typeof(a)) -- a | b)
   return a[Key]
end

If Roblox is able to address this issue, it would improve my development experience because at the moment I am writing my framework in the likeness of a knit, and in functions like GetService it would be good to use keyof

9 Likes

Has already been written up as a RFC and has been implemented into Luau, just waiting on Roblox now I believe: RFC: `keyof` and `rawkeyof` type operators by aatxe · Pull Request #16 · luau-lang/rfcs · GitHub

3 Likes

It has gone further.

7 Likes

What!! How??? Am I being deceived!?


Nope it is real, got merged a while ago.

@bitsplicer It being merged into Luau does not mean it’ll appear in Roblox Studio immediately or even around that time.

As I said when I linked that RFC in the first place, we usually have to wait for Roblox to implement it into Studio, and evidently, this claim holds up because it’s been about 2 months since it was merged and is just now being introduced.

1 Like

My point was never that it got added already, I was trying to show that since it is merged the process of being added has started.

Sorry for causing confusion.

1 Like

As a head’s up, even though the RFC has been merged and the feature has been implemented, it won’t be available within Studio for a while yet. The Luau team is working on a total rewrite of Luau’s type analyzer and keyof is only implemented in the new version.

I am told it will arrive “soon™” by team members on Discord, so make of that what you will.

8 Likes