Type Functions?

image
I thought these were only in the new solver? (I have the beta turned off)

This doesn’t throw a syntax error, how do I use it?

I believe this to be a bug. Normally functions in type annotation is:

type FunctionName = (--[[FunctionArguments]]) -> any --[[The type of the value returned by the function]]

And when I replicated the example shown in the image I could not get script completion to work as it was error type and it showed up as a generic type.

I believe they’re referring user defined type functions

Are you using --!strict? When I use it, I do get the syntax error saying it’s not supported. If you want an example on how to use them though, that link I posted above has some examples

1 Like

Here’s an example of how you could use your own type functions

--!strict
type function Pick(tbl, key)
	for k, v in tbl:properties() do
		if k == key then
			return v.read
		end
	end
end

type BType = Pick<{A: string, B: number}, "B"> -- BType will be a number
local X: BType = 10

1 Like

Even with !strict I do not get a syntax error. and didn’t know this existed that’s rad!

Dude… How does this work??
What the heck is :properties(), why do you return v.read??

I don’t know if Roblox has official documentation on this syntax yet. I’ve pretty much just copied my example from the RFC proposal link I posted previously.

Here’s what I found from that link about properties and v.read

  • properties() returns a table mapping property keys to a table with their respective read and write types
  • v.read is simply the read type of the property. Based on the rawget example they have, there can be cases where the read type and write type are different (I don’t know when this would be the case), but that’s why there are separate v.read and v.write fields.

So when returning v.read, we’re just saying we want to return that property’s type.