我正在从谷歌创建
webgl-utils.js的d.ts文件
我有一个问题,其中一个最后一行,全局对象中的方法是’猴子修补'(我认为这是正确的术语)
问题在于:
/** * Provides requestAnimationFrame in a cross browser way. */ window.requestAnimFrame = (function() { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(/* function FrameRequestCallback */ callback,/* DOMElement Element */ element) { window.setTimeout(callback,1000/60); }; })();
我如何在我的打字稿文件中声明这一点,以便在使用该函数时不会出现编译错误:
function tick() { requestAnimFrame(tick); drawScene(); }
我现在尝试过:
interface window { requestAnimFrame(): any; }
The name 'requestAnimFrame' does not exist in the current scope
解决方法
您正朝着正确的方向前进,但您需要定义所有变体:
interface Window { requestAnimFrame(callback: any,element?: any): void; webkitRequestAnimationFrame(callback: any,element?: any): void; mozRequestAnimationFrame(callback: any,element?: any): void; oRequestAnimationFrame(callback: any,element?: any): void; } window.requestAnimFrame = (function() { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback,element?) { window.setTimeout(callback,1000/60); }; })(); function tick() { window.requestAnimFrame(tick); }