Help with importing types efficiently?

I have a module holding every custom type my project uses, to save on declaring the same types at the top of every script.

Importing a type from the module is as simple as

local Types = require(<path>)
local Data: Types.EntityData = {}

The problem is, every time I use a type from the module I’m indexing the module. That’s a lot of Variable: Module.Type. Would I actually be better importing each type once and declaring a new type inheriting from the original template type, as in the case below?

local Types = require(<path>)
type EntityData = Types.EntityData
type ComponentData = Types.ComponentData
local Data: EntityData = {}

Or is it a case of ‘it really doesn’t matter’? On the one hand, I can see the standard format being less efficient since I’m indexing the module every time I use a type. On the other hand, I can see redeclaring every type I use for every script being inefficient too, since I’m flooding everything with a hierarchy of dummy types. Does anyone have any insight into the subject?

It’s a case of it doesn’t really matter. It’s your choice to decide what you’d like to use

Type annotations are disregarded when your code is compiled so there is no performance difference between the two (assuming that’s what you mean by “efficient”)

If you want my opinion, I think redefining types looks better, that way you don’t have to index the type through your types module every time you need to reference the type, making your type annotations shorter, but again that’s just my opinion


Perfect, thanks for clearing that up! Exactly what I was looking for.

1 Like