hazzard993 / love-typescript-definitions

Write LÖVE 2D projects with TypeScript
MIT License
116 stars 11 forks source link

l.g.setCanvas with stencil typing might not be properly defined #73

Closed praisethemoon closed 1 year ago

praisethemoon commented 1 year ago

Hello,

According this thread: https://love2d.org/forums/viewtopic.php?p=219664#p219664

It should be possible to call

love.graphics.setCanvas({canvas: this.canvas as Canvas, stencil: true});
image

Maybe I am doing somethign wrong, but if I // @ts-ignore it works.

Cheers

hazzard993 commented 1 year ago

Thanks for the issue, looks like setCanvas was missing a variant which I've added in v11.4.1. This object is a bit deceptive as it should be indexed with canvases and can be given properties like a standard object

Here's some ways to represent this in TypeScript:

const allowStencil: CanvasSetup = { stencil: true };
allowStencil[1] = this.canvas;

love.graphics.setCanvas(allowStencil);

This approach might help when initially trying to use setCanvas if the type-errors are not easily understood

If happy with the results, you could shift to what is effectively the same expression in TypeScript

love.graphics.setCanvas({ [1]: this.canvas, stencil: true });

NOTE: These are all indexed at 1 as they aren't considered "arrays" by TypeScript

This pattern also shows up deeper within CanvasSetup. I've put comments on the types for how to construct these objects and updated the CHANGELOG with some more info