How Can I Make Use Of: "<T...>", "::" and "|"

Hello, I recently checked out few public developer resources. I looked over the script’s contents and discovered certain things I hadn’t seen before. I was unable to find any resources on it, but I assume that it is some sort of type checking. Please continue if you are familiar with any documents or have an explanation. And I’d like to hear if this is useful. I appreciate your time.

Here is a quick example:

function Example<T...>(Text: string): string
	return Text
end

function Number(Value: number | any, ...: number)
	return Value + ...
end

local String = Example :: <T...>(Text: string) -> string

local Table = {} :: { (...any) -> () }
local Values = { ... } :: { any }
local Empty = nil :: Camera?

print(String(`Addition: {Number(1, 3)}`))

The “<T…>” truly attracts my curiosity.

1 Like

Roblox has documentation for this:

2 Likes

These are generic types. It allows you to basically create a variable which you can set (in arguments of function) and get (in output of function)

local function same<T>(argument: T): T
    return argument
end

-- types:
same(1): number
same("a"): string

This can be useful for making libraries, when you’re unsure of what the user will input. Example:

local function set<T>(object: T)
    return function(properties: T)
        for i, v in pairs(properties) do
            object[i] = v
        end
    end
end
set(workspace) {
    -- Autocomplete for Workspace is here
}
3 Likes

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