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?

4
  • Whats ctx? ..... Commented Nov 3, 2017 at 18:17
  • @Jonasw a Canva Context2D variable Commented Nov 3, 2017 at 18:18
  • So you want to extend the native CanvasRenderingContext2D by adding rounded_rect method? Commented Nov 3, 2017 at 18:25
  • @pawel yeah I guess so! Commented Nov 3, 2017 at 18:26

1 Answer 1

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...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.