imagekit-nodejs icon indicating copy to clipboard operation
imagekit-nodejs copied to clipboard

Return type of `listFiles` method should take into account input parameters

Open lorenzocestaro opened this issue 7 months ago • 2 comments

When calling listFiles the return type of the call is always FileObject | FolderObject regardless of the parameters passed to the listFiles call. This requires some extra type guards to process the response correctly in typescript, because some properties are exclusively present on files or folders.

For example:

// I would expect the type of resources here to be only FolderObject[]
const resources = await imagekit.listFiles({
  path: "/",
  type: "folder",
});

If I want to map over resources from above I will have to write some superfluous type guards to make sure TS knows we are working with Folders only. Yes, type-casting could be used, but I think this could be improved in the SDK types definitions

lorenzocestaro avatar Jun 02 '25 12:06 lorenzocestaro

We can update it to show union of both and make all fields optional. Would that be better?

imagekitio avatar Aug 14 '25 09:08 imagekitio

We decided not to merge the two fields into a union in order to maintain type safety. The core issue lies in the underlying API response, which needs improvement. We have already noted this for the V2 version.

If you have more ideas, you are welcome to add comments on this discussion - https://github.com/imagekit-developer/sdk-generation/discussions/3

imagekitio avatar Sep 21 '25 04:09 imagekitio