首页 > 解决方案 > 在 Typescript 严格模式函数上访问被调用者?

问题描述

我正在向这个库添加访问arugments.callee. 在项目的tsconfig.jsonI set"strict": false中,导致这个迷你测试工作:

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

这样可行。现在,如果我像这样导入要运行测试的库部分:

    import {isNumberInRange} from './is';

    function check() {
        console.log(arguments.callee.name);
    //    isNumberInRange(1,0,1);
    }
    check();

即使我实际上没有运行isNumberInRange函数 typescript 仍然会记录以下内容:

TypeError: 'caller'、'callee' 和 'arguments' 属性可能无法在严格模式函数或参数对象上访问以在检查时调用它们(/home/ole/Github/is/src/test.ts:4 :27)

我还需要做什么才能启用呼叫arguments.callee.name

标签: javascriptnode.jstypescript

解决方案


除了"strict": false还加"noImplicitUseStrict": true

TypeScript 会自己添加严格模式,这需要禁用。

如何做到这一点已经在这里得到解答:prevent-use-strict-in-typescript

您可以通过使用 --noImplicitUseStrict 编译器选项进行编译来做到这一点——将“noImplicitUseStrict”:true 添加到 tsconfig.json 中的“compilerOptions”。这样做会阻止编译器发出“use strict”。


推荐阅读