在导出函数中访问 'this' - 打字稿

accessing 'this' in exported function - typescript

我试图理解在导出函数中尝试 access/pass 'this' 时遇到的以下错误,我有以下代码:

export async function main() {
try {
    console.log(this)
catch (e: any) {

}

尝试编译时出现此错误:

src/main.ts:55:32 - error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.

55      console.log( this);
                                  ~~~~

src/main.ts:28:23
    28 export async function main() {
                             ~~~~
    An outer value of 'this' is shadowed by this container.

我不明白为什么我在访问它时遇到问题 - 'this' 应该是功能范围吗?还是全局对象?谁在隐藏这个变量,我该如何解决这个问题?

错误来自noImplicitThis 编译选项。 您可以删除此选项(不推荐)或声明 this 的类型,例如:

export async function main(this: unknown) {
    try {
        console.log(this);
    } catch (e: any) {

    }
}

游乐场link:https://tsplay.dev/mLLpbm