Creating an image compiles with an error in TypeScript
See original GitHub issueI’m trying to use Jimp with TypeScript in a following manner:
import * as Jimp from "jimp";
let image = new Jimp(256, 256, function (err, image) {
});
It produces the following errors:
severity: 'Error'
message: 'Only a void function can be called with the 'new' keyword.'
at: '2,13'
source: 'ts'
code: '2350'
severity: 'Error'
message: ''new' expression, whose target lacks a construct signature, implicitly has an 'any' type.'
at: '2,13'
source: 'ts'
code: '7009'
However, when I ignore the TypeScript compile-time errors and debug the result, it works just as expected, i.e. the image is created successfully. So it looks like we have a problem with jimp.d.ts. In fact, it describes the corresponding constructor not as a constructor, but as a regular function.
My main hypothesis is that d.ts file was prepared for an older version of TypeScript and uses different ways to describe Jimp
class (a function with a prototype). However, I’m new to TypeScript, so I may miss something.
I’ve tried playing around with module augmentation (using this technique for the first time, actually), but got a following error: Cannot augment module 'jimp' because it resolves to a non-module entity.
The project details are as follows.
- NodeJS environment.
- Jimp 0.2.28, installed with npm.
- TypeScript 2.6.2.
tsconfig.json
:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"sourceMap": true,
"strict": true,
"outDir": "dist",
"lib": [
"es5",
"es2015.iterable"
]
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:5
Top GitHub Comments
@brendtumi, you’ve just shown how to use static methods (
Jimp.read(buffer)
) to read an existing image.But I need to create an image from scratch, and there are several overloaded constructors for that in
jimp.d.ts
(which are not actually constructors, as they don’t compile withnew
), and there’s an explicit instruction to usenew Jimp(...)
for this case in the readme. Does it mean that the readme is outdated?You’re right, that’s nothing but a quick fix. I looked into it and created a pull request (#430) that should fix the issue.