-
-
Notifications
You must be signed in to change notification settings - Fork 765
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
error TS2497: Module ''jimp'' resolves to a non-module entity and cannot be imported using this construct. #581
Comments
# What's Changing and Why fixes typescript build ## What else might be affected closes #581 ## Tasks - [x] Add tests - [x] Update Documentation - [x] Update `jimp.d.ts` - [x] Add [SemVer](https://semver.org/) Label
released in v0.3.10 |
This actaully doesn't seem to work fully. Now when i try to import in a non ts file it is exported under default Do you know of any way to get this to work @atas ?
|
I gotta go back to because the latest release broke vscode's autocompletion. declare module 'jimp' {
export = Jimp.Jimp;
} I've found you can make <0.3.10 work with the following import Jimp = require('jimp'); |
I've found if you do either of the following in your tsconfig.json <0.3.10 "compilerOptions": {
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
}, So with that I am going to revert back to the code in 0.3.9 |
Hi, I've updated to the release v0.3.10 defined above but now I have bigger problems. Issues(1) If I set So I am not sure if adding (2) I tried (3) I tried (4) I tried Resolution Path
If you open the PR and add me as well I will contribute too @hipstersmoothie and will confirm what works compile and runtime. Intellisense may work or project may compile but there might be runtime errors still, so better to check it all the way just to be sure. Thank you, Ata |
Feel free to make a PR!! I’m pretty new to typescript and this change really seems to be like a one liner
Requirements:
* intellisense works in CJS project without having to `require('jimp').default`
* intellisense works in TSC project
* TSC can build the project without errors (and preferably without any special flags)
Did you try "allowSyntheticDefaultImports"?
I will devote more time to this today after work, but my limited typescript knowledge is showing (I've already spend 2-3 hours trying to fix this)
|
@atas ping |
Hey @hipstersmoothie , I will have a look on Monday and Tuesday, on holiday right now :) I actually got this working previously in a PR here #427 but that was closed in favour of another one which I just tested recently but I will go over again and create another PR soon. If you welcome the idea maybe we should tidy up the type definition file because it is done with a For instance, entry point is a class type and by using its static methods we return an instance of the class... That makes it difficult to extend as they are of the same type. You can define a class in typescript in ambient context and use it like a type definition but better to use interfaces, more flexible. Would you be open to a PR making that big of a change? |
I definitely open to a rewrite of the types file. I recently broke the reop into a mono repo structure where everything is now a plugin. It would be really cool if each plugin/type had it's own typedef file that extended the main jimp type file! |
I'd like to revive this issue for Jimp v0.5.2 with tsc v3.1.6. Currently I'm using |
Have you tried importing in other ways? @favna |
Actually yes, I have since figured out that it works with Extract:
IMO it feels off to be using |
Yeah i can write typescript code but have had no luck getting .d.ts files to work right all the time. The require feels off to me too. I've tried messing with the .d.ts file too with no avail. If anyone tries to fix this these requirements still stand: Requirements:
|
Not really an expert, but the following seems to fix most of my issues. Add the following to the top of the Let me know if this helps in any way. |
@hipstersmoothie it "works" but that's honestly stretching the definition. That it to say, the TS compiler throws this (replace "read" with whatever) on all functions and properties from Jimp and when excluding the That all said, I've done some more digging into declaration files myself and I've come a pretty clean solution of my own too.
If you give the green light I'll set up a PR for this solution. Edit: I have a branch with the change ready for PR here |
Sweet! Make a PR and I will get to it in the next few days! @favna |
@favna Nice, your solution is indeed working better. I am getting a TS error though: |
@pndewit ai... I didn't see that one. It does work when using it and I've compiled code successfully too. Maybe we should just add |
I'm cool with just ignoring it. I am reading a little bit about the issue we're trying to solve here and it seems like our solution is a little hacky though: https://stackoverflow.com/questions/39415661/what-does-resolves-to-a-non-module-entity-and-cannot-be-imported-using-this |
I'll add the ignore and create the PR but I'll keep this issue open (will rewrite the commit message to remove the "resolves" part). I think the ideal case would be to rewrite the declarations file, specifically by extracting the |
@kritollm please read issue comments before commenting yourself. It is known that your method works, as this is the documented method from typescriptlang (scroll to the section about require). The critique however is that using "require" (a CommonJS method of importing) is ugly when the rest of your code uses proper TypeScript supported ES6 Import/Export. Furthermore using this CommonJS scheme breaks the Babel loader used by jsdoc2md to parse JSDoc in TypeScript files. Suffice it to say it is a workaround for a problem that should not exist. This issue however gets entirely addressed by my PR #660 which will allow both |
* Improve TypeScript declarations file Addresses but does not fully close #581 Proper way to import is now `import * as X from 'jimp'` wherein X can be whatever the end-user wants, in most cases `Jimp`. * Truly resolve declarations file I have losely based this on knowledge gained from analyzing other declarations files, primarily @types/better-sqlite3. Using this formatting for declaration file allows for `import * as Jimp from 'jimp'` importing which is a proper way when using TypeScript as it primarily recommends ES6 imports. It will require setting the `allowSyntheticDefaultImports` compiler option to `true`, however this is a very small issue as many packages on npm (including aforementioned better-sqlite3) require this. - Like previous commit this ensures we no longer need to use `import Jimp = require('jimp'). As has been agreed upon, using `require` when working with ES6 modules is just wrong in code style. - Unlike previous commit no more ts-ignore, yay! - It is maintainable. New types or interface come at the bottom and any new or modifications to functions and constants go in the topmost interface. We'll never have to touch the declared const or the export.
* Improve TypeScript declarations file Addresses but does not fully close #581 Proper way to import is now `import * as X from 'jimp'` wherein X can be whatever the end-user wants, in most cases `Jimp`. * Truly resolve declarations file I have losely based this on knowledge gained from analyzing other declarations files, primarily @types/better-sqlite3. Using this formatting for declaration file allows for `import * as Jimp from 'jimp'` importing which is a proper way when using TypeScript as it primarily recommends ES6 imports. It will require setting the `allowSyntheticDefaultImports` compiler option to `true`, however this is a very small issue as many packages on npm (including aforementioned better-sqlite3) require this. - Like previous commit this ensures we no longer need to use `import Jimp = require('jimp'). As has been agreed upon, using `require` when working with ES6 modules is just wrong in code style. - Unlike previous commit no more ts-ignore, yay! - It is maintainable. New types or interface come at the bottom and any new or modifications to functions and constants go in the topmost interface. We'll never have to touch the declared const or the export.
Hi,
I cannot import jimp with typescript, getting error. Details are below.
Expected Behavior
There shouldn't be a compilation-time error.
Current Behavior
There is a compilation-time error.
error TS2497: Module ''jimp'' resolves to a non-module entity and cannot be imported using this construct. import * as jimp from 'jimp';
Also,
import jimp from 'jimp';
doesn't work because it imports the jimp into jimp_1 and becomes not usable...Failure Information (for bugs)
Steps to Reproduce
Import jimp in a typescript file with
import * as jimp from 'jimp';
Failure Logs
error TS2497: Module ''jimp'' resolves to a non-module entity and cannot be imported using this construct. import * as jimp from 'jimp';
The text was updated successfully, but these errors were encountered: