Skip to content
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

Improve TypeScript declarations file #660

Merged
merged 2 commits into from
Nov 28, 2018
Merged

Improve TypeScript declarations file #660

merged 2 commits into from
Nov 28, 2018

Conversation

favna
Copy link
Contributor

@favna favna commented Nov 19, 2018

What's Changing and Why

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.

As stated on the aforementioned issue another PR might come with a more thorough fix not requiring // @ts-ignore

What else might be affected

Method of importing with TypeScript, possible requires doc updates.

Tasks

  • Add tests
  • Update Documentation
  • Update jimp.d.ts
  • Add SemVer Label

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`.
@favna
Copy link
Contributor Author

favna commented Nov 19, 2018

@hipstersmoothie @pndewit tagging you since you're on the issue it addresses

declare module 'jimp' {
export = Jimp.Jimp;
}
// @ts-ignore

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your concern, but please read the corresponding issue as well. Technically the line is not needed as the compiler does not actually compile the type declarations. The ignore is exclusively for those people who open the file in their IDE which would report the structure as invalid. It is however, very much valid; if it wouldn't be then compiling would crash.

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.
@favna
Copy link
Contributor Author

favna commented Nov 27, 2018

@DanielRosenwasser @hipstersmoothie I've reworked it so it no longer requires the ts-ignore to satisfy the IDEs. Would appreciate another review. Copying from my Git commit message:

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 hurdle (and just a few clicks for the end-user) 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.


Lastly, I would really appreciate a new version to be pushed to npm if you guys decide to merge this. Due to your building setup I can't just yarn add favna/jimp#fix/typedeclarations to install my own branch so until a new proper build is released I'll have to use the old method of importing which also blocks me from updating my documentation as the jsdoc2md compiler cannot handle the = require structure.

@favna
Copy link
Contributor Author

favna commented Nov 27, 2018

Oh and one final note, if you guys decide to merge this I think #581 can be closed since the importing and exporting should be entirely fixed but I'll let that upon your judgement.

@hipstersmoothie
Copy link
Collaborator

@favna I will release once @DanielRosenwasser gives a 👍

@DanielRosenwasser
Copy link

Okay, I think that this change is appropriate. Users can use

import jimp = require("jimp")

to import the module.

Alternatively, if they have esModuleInterop they can use a default import:

import jimp from "jimp";

@DanielRosenwasser
Copy link

For the record, because Jimp is callable, import * as jimp from "jimp" shouldn't be used.

@hipstersmoothie
Copy link
Collaborator

hipstersmoothie commented Nov 28, 2018

@favna can you add @DanielRosenwasser usage comment to the docs in the "jimp" package readme? After that I'll get it merged and released.

@hipstersmoothie hipstersmoothie merged commit 8db9e24 into jimp-dev:master Nov 28, 2018
hipstersmoothie added a commit that referenced this pull request Nov 28, 2018
* 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.
@favna
Copy link
Contributor Author

favna commented Nov 28, 2018

Ow.. I see you merged already. Sorry I didn't add the usage doc to the readme yet. Long work days means I only just got home and you guys commented at my 2AM :\

I'll add the usage docs right now and create a new PR I guess.

@favna favna deleted the fix/typedeclarations branch November 28, 2018 19:21
@hipstersmoothie
Copy link
Collaborator

sorry! i merged then realized you hadn't done it. I'm using it in the CLI package. Releasing now in 0.6.0.

The only thing that seems to not quite work right is the font types. In some places I was using Jimp.Font and had to switch to any

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

error TS2497: Module ''jimp'' resolves to a non-module entity and cannot be imported using this construct.
3 participants