Oop typecasting help

Hey, so. I have a main module that requires the components and adds them to a table, well thats working good, but i would like to give those components auto completion [ when i index the table i want to see the options i can choose, after i choose one i want the object & constructor methods of the component to have autocompletion or intellisense how you want to call it] … now how would i do that? exactly typecasting, i have some issues on how to do that, in this code you can see my failed attempts.

Main Module:

export type BatObject = {
	Name : string,
	Equipped : boolean,
	Freq : number,
	User : Player,
	Components : {}, -- fix
	Power : number
}

type BatConstructor = {
	__index: BatConstructor,
	Type: Bat,
	create: (Attributes : BatObject) -> Bat,
	_createcomponent: (self: Bat, Component : ModuleScript) -> (),
	_createcomponents: (self: Bat) -> ()
}

local Bat = {} :: BatConstructor
Bat.__index = Bat
export type Bat = typeof(setmetatable({} :: BatObject, {} :: BatConstructor))

function Bat.create(Attributes : BatObject)
	local self = setmetatable({} :: BatObject, Bat)
	self.Name = Attributes.Name
	self.Freq = Attributes.Freq
	self.User = Attributes.User
	self.Components = {}
	self.Equipped = false
	self.Power = Attributes.Power
	self:_createcomponents()
	return self
end

function Bat:_createcomponent(Module : ModuleScript)
	local req = require(Module) :: any
	if req["create"] and req[Module.Name] then
		local component = req.create(self)
		type CommonType = typeof(req[Module.Name])
		self.Components[Module] = component :: CommonType
		print(Module.Name.." Sucessfully initialized")
	else
		warn("["..Module.Name.."]".."Invalid Component format EC [#001]")
	end
end

function Bat:_createcomponents()
	for _,module in next, script:GetChildren() do
		if module:IsA("ModuleScript") then
			self:_createcomponent(module)
		end
	end
end

return Bat

Example component:

--!strict

type Bat = typeof(require(game.ServerStorage:WaitForChild("Bat")).Type)

type SetupConstructor = {

__index: SetupConstructor,

create: (Bat : Bat) -> Setup,

equip: (self: Setup) -> (),

unequip: (self: Setup) -> (),

}

type SetupObject = {

Bat : Bat,

}

local Setup = {} :: SetupConstructor

Setup.__index = Setup

export type Setup = typeof(setmetatable({} :: SetupObject, {} :: SetupConstructor))

function Setup.create(Bat : Bat)

local self = setmetatable({} :: SetupObject, Setup)

self.Bat = Bat

return self

end

function Setup:equip()

if self.Bat.Equipped == false then

self.Bat.Equipped = true

end

end

function Setup:unequip()

if self.Bat.Equipped == true then

self.Bat.Equipped = false

end

end

return Setup

I’m not exactly sure if what you want is possible with the type system in place atm. The only way you may be able to get it working is by using generics and restructuring your code a bit so that it is clear that the types should be before runtime.

An issue with your code atm is that you are trying to assign type annotations at runtime:

	local req = require(Module) :: any
	if req["create"] and req[Module.Name] then
		local component = req.create(self)
		type CommonType = typeof(req[Module.Name]) -- This is only possible to figure out at runtime
		self.Components[Module] = component :: CommonType
		print(Module.Name.." Sucessfully initialized")
	else
		warn("["..Module.Name.."]".."Invalid Component format EC [#001]")
	end

So, i will have to make a non runtime type for the components table and assigning every component type manually?

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