react-lazily
react-lazily copied to clipboard
refactor: Simplify and enhance `lazily` function
Changes Summary
-
Type Definitions: Introduced more descriptive type definitions to make the code easier to read and understand.
-
ComponentName
to represent the name of a component as a string. -
Loader<T>
as a generic for the loader function that returns a promise of typeT
. -
NamedComponents
to represent a Record with component names as keys and their types as values.
-
-
Simplification: Removed the unnecessary generic constraint
U extends keyof T
as we are now enforcing the type throughNamedComponents
. -
Parameter Restructuring: Changed the
loader
function signature from(x?: string) => Promise<T>
to() => Promise<T>
, making it simpler and more specific to its purpose. -
Error Handling: Added an error-throwing mechanism if the requested component is not found in the module.
-
Type Casting: Explicitly cast
module[name]
toComponentType<T>
, which is more specific and provides better type checking.