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

Best practice for "generic" Jimp instance type missing from docs? #1328

Closed
LostInStatic opened this issue Sep 7, 2024 · 5 comments · Fixed by #1330
Closed

Best practice for "generic" Jimp instance type missing from docs? #1328

LostInStatic opened this issue Sep 7, 2024 · 5 comments · Fixed by #1330
Labels
released This issue/pull request has been released.

Comments

@LostInStatic
Copy link

LostInStatic commented Sep 7, 2024

Is your feature request related to a problem? Please describe.
I'm working on a solution that will load an image from url or a buffer, transform it and output it in base64. This means it will be passed around through a couple functions. I've tried to figure out a type for the Jimp object being passed as parameter, but nothing I tried works (see examples below). Resources from the internet show examples with older versions of Jimp (with default import).

AFAIK it was possible with older versions just by doing this:

import Jimp from "jimp"

function transform(jimpImage: Jimp) {
//...
}

Is it not possible anymore and I should treat url and buffer inputs as completely separate pipelines?

Describe the solution you'd like
Hopefully it's just a case of docs not being clear?

Additional context

Sandbox

import { Jimp } from "jimp";

// 'Jimp' refers to a value, but is being used as a type here.
async function transform(jimpImage: Jimp) {
  return await jimpImage.getBase64("image/png");
}

// Property 'getBase64' does not exist
async function transform1(jimpImage: typeof Jimp) {
  return await jimpImage.getBase64("image/png");
}

//Using just one return type will not accept the output of the other function
//Results in:
// This expression is not callable. Each member of the union type [...] has signatures, but none of those signatures are compatible with each other.
async function transform2(
  jimpImage:
    | Awaited<ReturnType<typeof Jimp.read>>
    | Awaited<ReturnType<typeof Jimp.fromBuffer>>
) {
  return await jimpImage.getBase64("image/png");
}
@LostInStatic
Copy link
Author

Haha, now I feel stupid - did not think about looking for InstanceType in TS docs even though this is literally what I've been thinking of. Thanks a lot!

@hipstersmoothie
Copy link
Collaborator

will be released soon!

@hipstersmoothie
Copy link
Collaborator

🚀 Issue was released in v1.4.0 🚀

@hipstersmoothie hipstersmoothie added the released This issue/pull request has been released. label Sep 7, 2024
@vntw
Copy link

vntw commented Sep 9, 2024

Hey @hipstersmoothie, thanks for all the improvements and new release 🚀

I upgraded to v1 without major issues but one question regarding the instance type: Should new Jimp(…) and await Jimp.read yield the same type? I used to pass a Jimp instance around interchangeably, which errors now. Hopefully this mini example shows what I mean (I also included the snippet of this issue in a separate file)

The docs still say that Jimp.read returns a Jimp instance - maybe a different one or just a type error?

Would be great to hear your thoughts on this, thanks! 🙂

@hipstersmoothie
Copy link
Collaborator

that's not a nice error! Would you mind opening a new issue with that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released This issue/pull request has been released.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants