Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the library tree shakable #31

Open
MickL opened this issue Feb 12, 2024 · 2 comments
Open

Make the library tree shakable #31

MickL opened this issue Feb 12, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@MickL
Copy link

MickL commented Feb 12, 2024

Using import { $enum } from "ts-enum-util"; will always import the full library even tho just one function of it is used.

Preferable it would be possible to import just what is needed, e.g.:

{ getValues } from "ts-enum-util";

enum ABC = {
    B = 3,
    A = 1,
    C = 2
}

// defined order is retained: [3, 1, 2]
const values = getValues(ABC);

Motivation: Both in the browser as in serverless (edge) functions bundle size is very important.

@UselessPickles
Copy link
Owner

I'll have to think on this for a while and experiment when I have some extra time, but my gut reaction is that this may not be possible, or the ways it could be possible may be too big of a downgrade in consistency and ease of use. This would be major rewrite of the entire library with a completely different TBD approach of how to get the generic types working as desired, so don't hold your breath for this. I will think about it and play around with some ideas when I have time.

This not just a library of individual functions that could be individually imported. A majority of this library is an EnumWrapper class whose constructor processes/caches some info about the enum, then has several methods. Then there's the $enum() function itself that that caches one instance of the wrapper per enum via a WeakMap, and and is needed to properly type the EnumWrapper. Much of this is intentionally designed this way for runtime optimization so you can simply use the library without worrying about caching and reusing instances of EnumWrapper, etc.

From a runtime implementation perspective, it's easy to imagine ways I could break this class up into an object of cached data per enum in the WeakMap and a collection of functions that all depend on that same object of cached data. However, the majority of the complexity of this library is in the generic types that make it all type-safe. I currently rely on the $enum() to set the type params of the EnumWrapper class correctly, which are then further referenced in generic methods of the class to enforce/infer types of additional params correctly. I would need to rethink the generic types from the ground up and come up with very different solutions to the problems that I specifically solved with the current pattern of the
$enum() function returning a class instance that has many methods. It may or may not be feasible to come up with a non-class-based solution that is easy to use.

@UselessPickles UselessPickles added the enhancement New feature or request label Feb 14, 2024
@exuanbo
Copy link

exuanbo commented Aug 21, 2024

Hi,

I've been considering this problem for a few days and recently published a library called ts-enum-utilx. It takes a different approach but also achieves type safety without the need for a wrapper class. The API is minimalistic, supports currying, and is fully tree-shakable.

I want to clarify that I really like the API design using the $enum() function and EnumWrapper class in ts-enum-util. My library just takes a different, more functional approach. It's not necessarily better, just different. The goals are also different.

Feel free to check it out and see if it meets your needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants