jimp-dev / jimp

An image processing library written entirely in JavaScript for Node, with zero external or native dependencies.
http://jimp-dev.github.io/jimp/
MIT License
13.95k stars 762 forks source link

`JimpInstance` type can't be used consistently when working with Jimp instances #1337

Open vntw opened 3 weeks ago

vntw commented 3 weeks ago

Expected Behavior

Before v1, it was possible to pass around Jimp as a type and throw in the results of Jimp.read/Jimp.create/new Jimp. Example without TS errors:

import Jimp from "jimp"

const img1 = await Jimp.read("")
const img2 = await Jimp.create("")
const img3 = new Jimp("")

function test(j: Jimp) {}

test(img1) // works
test(img2) // works
test(img3) // works

See https://codesandbox.io/p/sandbox/g56q6v?file=%2Findex.mts

Current Behavior

1330 already added an improvement via the JimpInstance type, but it seems like it doesn't work the same way (interchangeably).

import { Jimp, JimpInstance } from "jimp";

const inst = new Jimp({ width: 100, height: 100 });
const img = await Jimp.read("…");

function testInstance(j: JimpInstance) {}
function testRead(j: Awaited<ReturnType<typeof Jimp.read>>) {}

testInstance(img); // error
testRead(img);

testInstance(inst);
testRead(inst); // error

See https://codesandbox.io/p/sandbox/jimp-test-249ch8-pk8qdr?file=%2Findex.mts

Workaround for now is to use Awaited<ReturnType<typeof Jimp.read>>

Failure Information (for bugs)

Steps to Reproduce


Ref. https://github.com/jimp-dev/jimp/issues/1328#issuecomment-2337770442

phyzical commented 1 week ago

can confirm the workaround works for me

hipstersmoothie commented 5 days ago

Would either of you like to contribute this via PR?

hipstersmoothie commented 5 days ago

There may be a way we can massage the types to not need this an just use JImp as a type. I don't have too much time to figure that out tough