ARTICLE AD BOX
I am trying to detect the context type for an HTML canvas and I found the following code here on this website
// Store original code // (TS error 2551 property does not exist suggests 'getContext') HTMLCanvasElement.prototype._getContext = HTMLCanvasElement.prototype.getContext; // Store context type // (TS error 2339 property does not exist) HTMLCanvasElement.prototype._contextType = null; // Register getContext wrapper method // (TS error (2339 and 2551)) HTMLCanvasElement.prototype.getContext = function (type) { this._contextType = type; return this._getContext(type); } // Return the context type used // (TS error 2339 property does not exist, both statements) HTMLCanvasElement.prototype.hasContext = function () { return this._contextType; }Although the code compiles to working JavaScript which is accepted by google-closure-compiler TypeScript generates several errors - see comments in code for details.
I assume there is a 'TypeScript' way of adding properties to existing classes that I am unaware of. I have tried many alternatives including Object.assign and searched the Internet for a solution to no avail so I would appreciate any suggestions or even solutions.
