0

TypeScript: view on playground

alert = (function (origAlert) {
    return function (...messages: any[]) {
        origAlert(messages.join(" "))
    }
})(alert)

// Example
alert(1, 2)

I want to overwrite / redefine the function alert(message?: any) which has already been declared in lib.d.ts before: declare function alert(message?: any): void;

But alert = function... throws "Invalid left-hand side of assignment expression."

The point is that, function alert(...messages: any[]) { /* ... */ } would work* does not even work as well, but I need to use the original alert. And I'd dislike to define an extra const origAlert = alert before the function.

How can I do this?


Note that the compiled JavaScript in the Playground works as intended.

*on TypeScript Playground it works, but in Visual Studio it throws "Overload signatures must all be ambient or non-ambient"

1
  • I don't know what the intended use of your code is. However, if you need it for debugging, then I would just use console.log(). Otherwise, it's a bad idea to redefine native functions, often it backfires. Commented Dec 12, 2015 at 11:57

1 Answer 1

0
declare function alert(...messages: any[]): void;

window.alert = ((orig) => {
    return (...messages: any[]) => orig(messages.join(" "));
})(alert);

alert(1, 2)
Sign up to request clarification or add additional context in comments.

5 Comments

That hack works for this, thanks^^ I'll still hope someone can find a general solution
these error messages is not clear enough - look at github.com/Microsoft/TypeScript/issues/2474. I doubt that someone will make a general solution. P.S. I hope that I am wrong)
Also Playground is compiling it just as it should.. that's really strange
The compiler will output results even if there are transpilation errors, the flag noEmitOnError changes this behaviour.
It's compiled because of this hack do not owerwrites original lib.d.ts file declare function alert(message?: any): void; // this stuff following code tries to overwrite declare function like it's a variable alert = function(/**/){/**/} So, hacks is our everything. PS Sorry for engrish.

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.