question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript: Jimp types are not corresponding to Jimp implementation

See original GitHub issue

Expected Behavior

When importing Jimp as described in the doc for a TypeScript project:

import Jimp from 'jimp'

I should be able to use Jimp directly as is:

Jimp
  .read(fileData)
  .then((image: any) => {
    ...
  })
  .catch((err: any) => {
    ...
  })

Current Behavior

The compile step is OK, but there is runtime error saying:

TypeError: Cannot read property 'read' of undefined

If I use the require-like import syntax:

import * as Jimp from 'jimp'

and use Jimp as below:

Jimp
  .read(fileData)
  .then((image: any) => {
    ...
  })
  .catch((err: any) => {
    ...
  })

I then have a compile error saying:

Property 'read' does not exist on type 'typeof import("/path/to/node_modules/jimp/types/ts3.1/index")'

If I disable manually type checking for Jimp:

// @ts-ignore
Jimp
  .read(fileData)
  .then((image: any) => {
    ...
  })
  .catch((err: any) => {
    ...
  })

There is no runtime error and Jimp works as expected.

Failure Information (for bugs)

Context

  • Jimp Version: 0.8.4
  • Operating System: macOS 10.15 Catalina
  • Node version: 10.16.3
  • NPM Version: 6.11.3
  • TypeScript Version: 3.6.3

Workaround

To use Jimp with types information, I have to import the types separately and bind them to the classic require syntax:

import Jimp from 'jimp'
// tslint:disable-next-line: no-var-requires
const jimp: Jimp = require('jimp')

And then, use “jimp” instead of “Jimp”:

jimp
  .read(fileData)
  .then((image: any) => {
    ...
  })
  .catch((err: any) => {
    ...
  })

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:26 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
crutchcorncommented, Nov 26, 2019

Are y’all able to test against 0.9.2 stable release. There was some work done to improve TS stability and should fix everyone’s issues with typings (if you were one of the ones that had to revert to 0.6.4, this includes you). Please test against and confirm/deny crosses fingers

1reaction
crutchcorncommented, Mar 28, 2020

Oh gotcha I see what you’re up to! Love what you’ve been able to do thus far. If we’re able to get some stuff into our type tests, I’d appreciate that as well! I think once you have more tweaks (as you’d mentioned, I didn’t have any notes at a first glance), I’ll be able to review the PR today and we can probably get it moved forward.

Thanks so much for jumping in and helping! 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jimp - npm
An image processing library for Node written entirely in JavaScript, with zero native dependencies. The default jimp configuration. Supported  ...
Read more >
Jimp: image processing fail on module usage - Stack Overflow
I'm using import * as Jimp from 'jimp' and that's working fine. I'm using webpack, but it should not be different in TypeScript/Angular....
Read more >
Documentation - TypeScript 4.9
In the editor, when running a go-to-definition on the return keyword, TypeScript will now jump you to the top of the corresponding function....
Read more >
TypeScript | IntelliJ IDEA Documentation - JetBrains
IntelliJ IDEA supports developing, running, and debugging TypeScript source code. IntelliJ IDEA recognizes .ts and .tsx files and provides full range of ...
Read more >
Code Navigation in Visual Studio Code
Tip: You can open any file by its name when you type Ctrl+P (Quick Open). ... Tip: You can jump to the definition...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found