Hi, I want to create a class that has an attribute which is a table containing objects of another class (which I called NestedClass), indexed by a string. I’m pretty new to type checking am I doing something wrong here?
– MyClass
--!strict
local nestedClass = require(script.NestedClass)
type nestedType = nestedClass.NestedClassType
local MyClass = {}
MyClass.__index = MyClass
function MyClass.new()
local self = setmetatable({}, MyClass)
self.nestedObjects = {} :: {[string] : nestedType}
return self
end
function MyClass:setNestedObjectA(key : string, a:string) : ()
if not self.nestedObjects[key] then
self.nestedObjects[key] = nestedClass.new(a)
else
return self.nestedObjects[key]:SetA(a)
end
end
return MyClass
– Nested Class
--!strict
local NestedClass = {}
NestedClass.__index = NestedClass
function NestedClass.new(a: string)
local self = setmetatable({}, NestedClass)
self.a = a
return self
end
function NestedClass:SetA(a:string): ()
self.a = a
end
export type NestedClassType =
typeof(NestedClass)
return NestedClass
It gives me an error on line 20 of MyClass
