typescriptdebuggingstrict

TypeScript complains «'callee' may not be accessed on strict mode functions» even with alwaysStrict set to false


I'd like to be able to pepper an app with a custom log function when debugging, profiling or just familiarizing myself with the code, using arguments.callee.name so I don't have to type the enclosing function by hand, so all I have to do is paste myProfilingFunc(arguments.callee.name); anywhere. It works a treat in JavaScript (without strict mode), but in TypeScript I get:

TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them

even after disabling every strict-related option on tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2017",
    "module": "commonjs",
    "lib": [
      "es2017"
    ],
[...]
    "strict": false,
    "alwaysStrict": false,
    "noImplicitUseStrict": true
  }
}

This seems to have worked back in 2019, but not anymore.

Minimal example:

function testCallee() {
    console.log(arguments.callee.name);
}

testCallee();

Open in TS Playground


Solution

  • Those are now deprecated. If what you want is the name of the function stored in the callee do the following:

    function testCallee() {
        console.log(testCallee.name);
    }
    
    testCallee();

    As an alternative, you can make use of the Error object and write a function that extracts whatever information you need from it.

    function testCallee() {
       const error = new Error();
       const reg = new RegExp(/at\s+((\S+)\s)?\((\S+):(\d+):(\d+)\)/);
       const stact = error.stack?.split('\n').at(1);
       const caller = reg.exec(stact);
       console.log(caller);
    }
    testCallee();