TypeScript中的覆盖功能

编程入门 行业动态 更新时间:2024-10-25 18:28:23
本文介绍了TypeScript中的覆盖功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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中的覆盖功能

本文发布于:2023-06-12 05:14:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/651993.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:功能   TypeScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!