react-lazily icon indicating copy to clipboard operation
react-lazily copied to clipboard

refactor: Simplify and enhance `lazily` function

Open romantech opened this issue 1 year ago • 0 comments

Changes Summary

  1. 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 type T.
    • NamedComponents to represent a Record with component names as keys and their types as values.
  2. Simplification: Removed the unnecessary generic constraint U extends keyof T as we are now enforcing the type through NamedComponents.

  3. Parameter Restructuring: Changed the loader function signature from (x?: string) => Promise<T> to () => Promise<T>, making it simpler and more specific to its purpose.

  4. Error Handling: Added an error-throwing mechanism if the requested component is not found in the module.

  5. Type Casting: Explicitly cast module[name] to ComponentType<T>, which is more specific and provides better type checking.

romantech avatar Sep 15 '23 11:09 romantech