You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
which works fine for adding type annotations to the base class. However, I can't figure out how to add type annotations to the subclass. I've searched the web and haven't found anyplace where this is discussed, so I'm hoping someone here might know where I can find a working example of this. My first instinct was to try something like:
-- Base class (works):
type WorldObjectProto = {
__index:WorldObjectProto,
new:(id:number, inst:Instance?) -> WorldObject,
setId:(self:WorldObject, id:number) -> ()
}
type WorldObject = typeof(setmetatable({}::{ ObjId:number, Inst:Instance?}, {}::WorldObjectProto))
-- Inherited class (does not work):
type PlayerObjectProto = typeof(setmetatable({}::{
__index:PlayerObjectProto,
new:(id:number, inst:Instance?, player:Player?) -> PlayerObject,
destroy:(self:PlayerObject) -> ()
}, {}::WorldObject));
type PlayerObject = typeof(setmetatable({}::{
Player:Player?
}, {}::PlayerObjectProto))
It tells me I can't cast { } to WorldObject and PlayerObjectProto, I guess because the empty table there is missing all the members described in the type. Is my only option to describe the subclass as its own type without any inheritance, and just duplicate all the members and properties from the base class?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm attempting to use luau type checking for a simple class hierarchy, like the one described here in the Lua book:
https://www.lua.org/pil/16.2.html
I've used the technique described in the Luau getting started guide here:
https://luau-lang.org/typecheck#adding-types-for-faux-object-oriented-programs
which works fine for adding type annotations to the base class. However, I can't figure out how to add type annotations to the subclass. I've searched the web and haven't found anyplace where this is discussed, so I'm hoping someone here might know where I can find a working example of this. My first instinct was to try something like:
It tells me I can't cast { } to WorldObject and PlayerObjectProto, I guess because the empty table there is missing all the members described in the type. Is my only option to describe the subclass as its own type without any inheritance, and just duplicate all the members and properties from the base class?
Thanks so much!
Beta Was this translation helpful? Give feedback.
All reactions