Let’s say I want to make a class, it will look something like this:
local Wheel = {}
Wheel.__index = Wheel
function Wheel.new(config)
local self = setmetatable({}, Wheel)
self.part = config.part
self.isSteer = config.isSteer
self.angularVelocity = 0
return self
end
return Wheel
Easy, right? What about typing support? Let’s add it…
local Wheel: Types.Impl = {} :: Types.Impl
Wheel.__index = Wheel
function Wheel.new(config: Types.ConstructorConfig)
local self = setmetatable({} :: Types.Proto, Wheel)
self.part = config.part
self.isSteer = config.isSteer
self.angularVelocity = 0
return self
end
That’s not all yet…
--!strict
local EngineDef = require(script.Parent.Parent.types.engine)
export type Impl = {
__index: Impl,
-- Constructor
new: (config: ConstructorConfig) -> Wheel,
-- External
getAngularVelocity: (self: Wheel) -> number,
getLength: (self: Wheel) -> number,
getMaxLength: (self: Wheel) -> number,
-- Internal
_init: (self: Wheel, config: InitConfig) -> (),
_applyForce: (self: Wheel, config: ForceInfo) -> (),
_computePosRot: (self: Wheel, config: VisualInfo) -> ()
}
export type Proto = {
-- External
part: BasePart,
isSteer: boolean,
visualize: boolean,
steer: number,
rotation: number,
angularVelocity: number,
raycastParams: RaycastParams,
positionOffset: CFrame,
weld: Weld,
radius: number,
maxSteerAngle: number,
friction: number,
spring: SuspensionSpring,
engine: EngineDef.Engine
}
export type ConstructorConfig = {
part: BasePart,
isSteer: boolean
}
export type InitConfig = {
positionOffset: CFrame,
raycastParams: RaycastParams,
weld: Weld,
radius: number,
maxSteerAngle: number,
friction: number,
springHeight: number,
springStiffness: number,
springDamping: number,
engine: EngineDef.Engine
}
export type SuspensionSpring = {
length: number,
maxLength: number,
stiffness: number,
damping: number
}
export type ForceInfo = {
root: Part,
deltaTime: number,
rotationTransform: CFrame?,
throttleFloat: number,
throttle: number,
torque: number,
steerFloat: number,
antiRollBarForce: number
}
export type VisualInfo = {
root: Part,
steerFloat: number,
deltaTime: number
}
export type Wheel = typeof(setmetatable({} :: Proto, {} :: Impl))
return {}
Well, by this point you should see the issue, so how do I get typing support without this type declaration file monster? In C# it would be simple as what shown below (and it would get type support out of the box):
public struct Config {
public double angularVelocity;
public boolean isSteer;
public Part part;
}
public class Wheel {
public double angularVelocity;
public boolean isSteer;
public Part part;
Wheel(Config config) {
part = config.part;
isSteer = config.isSteer;
}
}