'Typescript compiler error for property that exists
I am using a React
package called react-canvas-draw
(https://github.com/embiem/react-canvas-draw) and the main class CanvasDraw
has a function called getDataURL
. I know it does because I can see it in the code:
_defineProperty(_assertThisInitialized(_this), "getDataURL", function (fileType, useBgImage, backgroundColour)
The problem is that the type declaration doesn't have the function signature so my code doesn't see it as a function that can be called. This is in the @types/react-canvas-draw/index.d.ts
file.
I've tried to manually edit the index.d.ts
file to include the signature like so:
getDataURL(fileType: string, useBgImage:boolean, backgroundColour:string): string;
and this does appease the editor so it accepts the method when I'm editing.
When I save, however, and it compiles it throws the following error:
Property 'getDataURL' does not exist on type 'CanvasDraw'
My code that is calling the method is from a ref
that I know works because I have called other methods off the object, undo()
for example. It is:
canvasDrawRef.getDataURL('png', false, '0xffffff');
Is there any way I can force this method to be called at runtime and prevent the compiler from throwing the error? And is there anything else I can do to declare the function as callable?
Solution 1:[1]
I was able to quieten the compiler with the following line just above the code that I wanted it to ignore:
// @ts-ignore: Unreachable code error
I would only recommend doing this if you are sure the code is going to work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Lee Probert |