When trying to achieve inheritance through both standard Luau as well as custom types, attempting to access a property from an instance of the derived class will result in a Type <custom_class> does not have key <class_property> error.
Despite this error…
Steps to replicate (alternatively, download the attached place file):
- Set
workspace.UseNewLuauTypeSolverto “Enabled”. - (Optionally) Set
workspace.LuauTypeCheckModeto “Strict”. - Create two
ModuleScriptinstances parented to the same folder:Base
--!strict const Base = {} Base.__index = {} export type Class = setmetatable<{ read instance: Instance, }, typeof(Base)> function Base.new(instance: Instance): Class return setmetatable({ instance = instance, }, Base) end function Base.ChangeName(self: Class, name: string): () error("Override required.") end return BaseDerived
--!strict const Base = require(script.Parent.Base) const Derived = setmetatable({}, Base) Derived.__index = Derived export type Class = setmetatable<Base.Class & { -- ...add new members here. }, typeof(Derived)> function Derived.new(instance: Instance): Class return table.freeze(setmetatable(Base.new(instance), Derived)) end function Derived.ChangeName(self: Class, name: string): () self.instance.Name = name -- TypeError: Type 'Class' does not have key 'instance' end return Derived
you dun did it now
Repro62006667_Type does not have key.rbxl (60.2 KB)
System Information
CPU: 12th Gen Intel(R) Core(TM) i9-12900K (16+8)
GPU 1: NVIDIA RTX PRO 4000 Blackwell SFF Edition
GPU 2: Intel UHD Graphics 770 @ 1.55 GHz [Integrated]
Memory: 62.47 GiB
Expected behavior
Based on the example classes from earlier, I expect Derived to inherit the properties & functions of Base through the use of metatables. Each of these classes reside in their own ModuleScript instance and their types are referable through a <module>.Class syntax.
In the context of the issue example, self.instance should be a valid member of class Derived when self is type-hinted with its custom Class type, like autocomplete suggests.
