In my AWS lambda functions, I have a variety of modules like UserModule
, NotificationsModule
, CompanyModule
, and more. To simplify the usage of these modules, I created an interface that outlines their structure as shown below:
interface Modules {
company: ICompanyModule
user: {
base: IUserModule
}
notifications: {
settings: INotificationSettingsModule
details: INotificationDetailsModule
}
}
When a function requires specific modules such as the company and user base module, I currently need to import and instantiate them manually. However, with multiple functions, this process becomes cumbersome. To streamline this, I decided to implement a factory
method that returns only the necessary modules for each function. For example:
// Single module
const module = factory('company') // Type would be ICompanyModule
// Multiple Modules
const modules = factory(['company', 'user.base']) // Type would be { company: ICompanyModule, 'user.base': IUserModule }
The input for the factory method is an array of keys from the Modules
interface.
As I attempted to resolve this issue, I encountered a
Type instantiation is excessively deep and possibly infinite
error. Here's the current implementation:
// Code snippet provided
...
Although the code works correctly in providing the expected object structures, Typescript flags an error due to excessive recursion likelihood when dealing with arrays of paths in the ReturnType
. After spending some time trying to address this issue, it seems that Typescript struggles with handling cases where the passed array could potentially lead to infinite recursion.