How would I check to see if an instance is a custom type

So I have this custom type that is supposed to be returned by a function, but I am not sure how to check if the right type is returned.

Example of function:

export type testType = {
      property : number
}

local testFunc()
     return {["property"] = 10}
end

Reveiving end

local testValue = testFunc()

if typeof(testValue) == --[[idk what to do here]] then
       do smth
end

I am just confused as to how I would check the type to make sure it matches the custom one. I was thinking it would be a function that checks the table’s keys, but I wanted to make sure there wasn’t a better way to do it.

bumping topic bc nobody answered yet

I use newproxy to generate a unique userdata and internally store it.

local something = {}
local proxy: any = newproxy()

export type something = typeof(setmetatable({} :: somethingClass, {} :: somethingMetatable))
export type somethingClass = {}
export type somethingMetatable = {
    _proxy: any
}

function something.new(): something
    return setmetatable({}, {
        _proxy = proxy
    })
end

function something.is(any: any): boolean
    return if getmetatable(any) ~= nil then getmetatable(any)._proxy == proxy else false
end

return something

-- somewhere
local class = something.new()
print(something.is(class)) -- true

Edit: Otherwise, you can internally store all classes made with your constructor function for a “guaranteed” source of truth, but it’s easier for me to use proxies.

1 Like

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