打字稿:箭头函数的内部参数

Typescript: this inside parameters of arrow function

我熟悉 this 在打字稿中的箭头函数内的行为(或者至少我是这么认为的)。 然而今天我偶然发现了一个 this 被用在箭头函数的参数列表中(这是来自 alexa-sdk 的类型定义):

export interface Handlers<T> {
    [intent: string]: (this: Handler<T>) => void;
}

这到底是什么意思,我将如何实施?

let handlers: Handlers<IntentRequest> = {
    "MyIntent" = ???
}

我知道我可以做类似的事情:

let handlers: Handlers<IntentRequest> = {
        "MyIntent" = function() {
           let self: Alexa.Handler<IntentRequest> = this;
        }
}

但是没有带箭头函数的self/this赋值有没有更优雅的解决方案?

在较新版本的打字稿中,您可以将其类型指定为函数的伪参数

let handlers: Handlers<IntentRequest> = {
    "MyIntent": function (this: Handlers<IntentRequest> /* , realParam: string */) {
       // this will have the type Handlers<IntentRequest>
    }
}