Luau type hints and the Luau runtime are entirely separate. They’re hints, they don’t do anything other than inform developers what an object should be.
Here’s an example type:
type Point = {
X: number,
Y: number
}
This defines a table that should contain an X and Y property. However, its fundamental type is still a table. This is why when you run typeof on a table conforming to this interface, it still returns "table".
Here’s an example function:
function createPoint(X: number, Y: number): Point
return {X = X, Y = Y}
end
This function returns a table. But you know that the table is guaranteed to conform to the Point interface. However, the runtime doesn’t know that. Only you know that.
What's the runtime?
The runtime is the thing that runs your code. Luau has to compile and then interpret your code in order for it to do anything. Otherwise, it’s just a meaningless text file.
Type information is (probably) lost at the compilation step since Luau doesn’t need type information to run your code.
Possibly by using the type() function from lua and making your own function for detecting if the table is your custom datatype. Also, why would you need custom datatypes.
Roblox currently doesn’t allows Developers (yet) to check the Custom Type of a Certain Object/Table. The only way you could do it is by creating a custom type function. I would recommend you creating a variable inside a certain Object’s metatable and name it __type, Then you can access it via getmetatable().
Here’s what it would look like:
-- Example:
local SomeTable = {}
-- Setting the SomeTable's metatable to this:
setmetatable(SomeTable, {
__type = "SomeTable"
})
-- The custom type function:
local function GetType(Target)
return getmetatable(Target).__type or typeof(Target)
end
-- Testing:
print(GetType(SomeTable)) -- Prints "SomeTable".
why is thing giving me nil when i say “Y” Is it because metatable?
type MyType = {
Y: number,
X: number,
YD: number,
XD: number,
}
local MyType = {}
function MyType.new(Y,X,YD,XD): MyType
local new = {Y,X,YD,XD}
setmetatable(new,{__type = "MyType"})
return new
end
local function GetType(value: any):string
return getmetatable(value).__type or typeof(value)
end
local d = MyType.new(1,3,4,5)
print(GetType(d))
print(d.Y) -- this prints nil
print(d)
type MyType = {
Y: number,
X: number,
YD: number,
XD: number,
}
local MyType = {}
function MyType.new(Y,X,YD,XD): MyType
local new: MyType = {Y = Y, X = X, YD = Y,XD = XD}
setmetatable(new,{__type = "MyType"})
return new
end
local function GetType(value: any):string
return getmetatable(value).__type or typeof(value)
end
local d = MyType.new(1,3,4,5)
print(GetType(d))
print(d.Y)
print(d)