Skip to content

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

@LostInStatic

Description

@LostInStatic

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");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    releasedThis issue/pull request has been released.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions