0

I'm familiar with the behaviour of this inside an arrow function in typescript (or at least I thought so). However today I stumbled across a this being used in the arguments list of an arrow function (this is from the type definitions of the alexa-sdk):

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

What does this actually mean and how would I implement this?

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

I know that I can do something like:

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

but is there a more elegant solution without the self/this assignment with a arrow function?

1

1 Answer 1

2

In newer versions of typescript you can specify the type of this as a pseudo paramter to the function

let handlers: Handlers<IntentRequest> = {
    "MyIntent": function (this: Handlers<IntentRequest> /* , realParam: string */) {
       // this will have the type Handlers<IntentRequest>
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.