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

[typescript] wrong types on getContext() #1783

Closed
doritosxxx opened this issue Apr 14, 2021 · 1 comment
Closed

[typescript] wrong types on getContext() #1783

doritosxxx opened this issue Apr 14, 2021 · 1 comment

Comments

@doritosxxx
Copy link

Issue

Possibly part of #1656

getContext() returns type NodeCanvasRenderingContext2D. NodeCanvasRenderingContext2D extends CanvasRenderingContext2D, which has the canvas field.

canvas field has type HTMLCanvasElement. But as far as i know this library doesn't work with DOM. Furthermore HTMLCanvasElement can't be explicitly converted to Canvas.

Steps to Reproduce

import { Canvas } from 'canvas'

const canvas = new Canvas(100, 100)
const ctx = canvas.getContext("2d")

// cts.canvas is HTMLCanvasElement accoring to typings.
// but it should be Canvas.
// HTMLCanvasElement has the "toBlob" method

// TypeError: ctx.canvas.toBlob is not a function
ctx.canvas.toBlob(()=>{})

// This one won't compile.
ctx.canvas.toBuffer()

// This one works fine.
;(ctx.canvas as unknown as Canvas).toBuffer()

Your Environment

@zbjornson
Copy link
Collaborator

Yeah, for the sake of simplicity I had just extended CanvasRenderingContext2D, but as you found in #1656, that turned out to be problematic.

Happy to review a PR if you want to work on this. Meanwhile I'm going to close this as a duplicate of #1656.

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

No branches or pull requests

2 participants