0

I am trying to convert a javascript code file to a typescript file, however, I have difficulties doing it when meeting to modify a function through the variable reference.

In javascript, the code was written as:

  Object.getPrototypeOf(ctx).rounded_rect = function(){

  }

where ctx is a canvas rendering context 2d varibale

How do I convert this code to typescript code? When I copy and paste it to typescript file, it shows error saying Property 'rounded_rect' does not exist on type 'CanvasRenderingContext2D'.

Also, what is this line of code called?

Leo Li
  • 73
  • 1
  • 3
  • 10

1 Answers1

1

You need to extend the native prototype which requires a declaration first:

declare global {
   interface  CanvasRenderingContext2D {
      rounded_rect() : void;
   }
}

After that you can implement it:

CanvasRenderingContext2D.prototype.rounded_rect = function(){
 /*whatever*/
};

May refer to this similar answer...

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151