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

use tailored types instead of extending DOM #2174

Merged
merged 1 commit into from
Dec 22, 2022
Merged

Conversation

chearon
Copy link
Collaborator

@chearon chearon commented Dec 14, 2022

The properties are ordered the same as their implementations, and I looked at each to make sure the signatures match. There are a lot of arguments in the DOM types that weren't supported by node-canvas, so this feels like the right way.

I tried to be careful, but this was a lot. I checked that every example file passes // @ts-check. Even if there are no mistakes, since these new types are smaller, someone could have been passing arguments that node-canvas wouldn't recognize and it would break their typescript build.

Fixes #1656

Copy link
Collaborator

@LinusU LinusU left a comment

Choose a reason for hiding this comment

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

This seems like a good way forward 👍

Copy link
Collaborator

@zbjornson zbjornson left a comment

Choose a reason for hiding this comment

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

👍

@@ -80,7 +79,7 @@ export class Canvas {

constructor(width: number, height: number, type?: 'image'|'pdf'|'svg')

getContext(contextId: '2d', contextAttributes?: NodeCanvasRenderingContext2DSettings): NodeCanvasRenderingContext2D
getContext(contextId: '2d', contextAttributes?: NodeCanvasRenderingContext2DSettings): CanvasRenderingContext2D
Copy link
Collaborator

Choose a reason for hiding this comment

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

This probably means a semver-major release, right?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, might be worth keeping the old name then? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We could also export aliases to the old names. I thought the "NodeCanvas" prefix was to avoid naming conflicts from lib.dom.ts.

Even then, it still could break someone's build if they were making function calls with arguments we ignore. Not sure if that's as big of a deal though?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not at all opposed to a semver-major release. This would be an easy one for users to handle. Just want to be on the same page.

Other question is usability - users could alias the type themselves (import type {CanvasRenderingContext2D as NodeCanvasRenderingContext2D}), but is it more convenient for the provided name to be NodeCanvasRenderingContext2D?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I prefer the shorter name. We call it Canvas everywhere else, not NodeCanvas. Our main users are using node, so it shouldn't cause a naming conflict should it? If they do have DOM types, the imported type would take priority over the ambient types, which is what you'd expect. I'm not 100% certain it's the right call, but pretty sure it would be fine...

Copy link
Collaborator Author

@chearon chearon Dec 18, 2022

Choose a reason for hiding this comment

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

Plus we already kind of did this. You can import {Image} which would conflict with the browser-defined Image.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fair point, same for the the gradient and pattern classes.

@LinusU good with you? Shorter name and semver-major?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, it seems like the old code exported without the Node prefix, so I'm not sure this is a breaking change?

export { NodeCanvasRenderingContext2D as CanvasRenderingContext2D }

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's true. I think it would still let you import {NodeCanvasRenderingContext2D} from 'canvas' because it's a .d.ts. Could still export both. IMO the chances of this breaking someone's build are small as-is and we can only make it smaller but not get to 0.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good point! Makes more sense as semver-minor then.

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.

TypeScript typings pollute global types by including lib.dom.d.ts
3 participants