-
-
Notifications
You must be signed in to change notification settings - Fork 141
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
Object property enumeration methods used on enum reporting as unused exported enum members #699
Comments
Knip doesn't really support any kind of dynamics like the one reported. It's basically mostly just direct references or "dotted" references (for e.g. namespaces, enum members). However, perhaps we could such handle situations a bit like https://knip.dev/guides/namespace-imports and when an enum (or namespace) is used as an argument in eg. |
This situation does sound a lot like the namespace example. As noted there, you don't really know if the namespace is being used by To elaborate a bit on where this presented itself, we have scenarios where the enum members needed to be turned into an array of strings for validation purposes (e.g. mongoose schema enum constraints). The enum members might not need to be referenced directly since they might simply flow in and out as the initialized strings. The enum makes a nice container for these values and when necessary, individual enum members could be referenced, even though they all don't have to be. |
I have the exact same situation and one more where it is used as a type only: export enum Gender {
MALE = 'male',
FEMALE = 'female',
DIVERS = 'divers',
ANY = 'any',
}
export const genders = Object.values(Gender)
export interface Profile {
gender?: Gender
} |
I've created a reproduction at https://github.com/shawnmcknight/knip-enum-object-values.
In this scenario, there is an
enum
with initializers:That enum is imported and run through any of the Object property enumerators:
With this code, each enum member is reported as unused by knip, but effectively every enumerated value is being used. I'm not sure I would consider
Object.keys
to be "using" the enum member, but bothObject.values
andObject.entries
are consuming the entirety of the enumeration.Please let me know if there is anything else I can provide to assist!
The text was updated successfully, but these errors were encountered: