Search code examples
javascripttypescriptuserscriptstampermonkey

How to 'return' outside function definition in typescript


  1. I need to rewrite my tampermonkey script on typescript.

  2. What is correct way to rewrite this javascript on typescript?

    if (window.top != window.self ) {
        return;
    }
    

Main goal is to stop executing script if it's running not in window.top.

Typescript compiler write ERROR TS1108: A 'return' statement can only be used within a function body.


Solution

  • You cannot return outside function, but using throw Exception you can stop execution userscipts (tampermonkey) too:

    if (window.top != window.self) {
        throw new Error("Not on top");
    }