TypeScript:在操场上观看
TypeScript: view on playground
alert = (function (origAlert) { return function (...messages: any[]) { origAlert(messages.join(" ")) } })(alert) // Example alert(1, 2)我要覆盖/重新定义在lib.d.ts中已经声明过的函数alert(message?: any):declare function alert(message?: any): void;
I want to overwrite / redefine the function alert(message?: any) wich has already been declared in lib.d.ts before: declare function alert(message?: any): void;
但是alert = function...会抛出" Invalid left-hand side of assignment expression. "
But alert = function... throws "Invalid left-hand side of assignment expression."
问题在于,function alert(...messages: any[]) { /* ... */ } 可以正常工作 *甚至不能正常工作,但是我需要使用原始的alert.而且我不希望在函数之前定义一个额外的const origAlert = alert.
The point is that, function alert(...messages: any[]) { /* ... */ } would work* does not even work aswell, but I need to use the original alert. And I'd dislike to define an extra const origAlert = alert before the function.
我该怎么做?
请注意,在Playground中编译的JavaScript可以按预期工作.
Note that the compiled JavaScript in the Playground works as intended.
*在TypeScript Playground上可以运行,但是在Visual Studio中会抛出" Overload signatures must all be ambient or non-ambient "
*on TypeScript Playground it works, but in Visual Studio it throws "Overload signatures must all be ambient or non-ambient"
推荐答案declare function alert(...messages: any[]): void; window.alert = ((orig) => { return (...messages: any[]) => orig(messages.join(" ")); })(alert); alert(1, 2)
更多推荐
TypeScript中的覆盖功能
发布评论