Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9163469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:27:20+00:00 2026-06-17T14:27:20+00:00

I have a TypeScript class, with a function that I intend to use as

  • 0

I have a TypeScript class, with a function that I intend to use as a callback:

removeRow(_this:MyClass): void {
    ...
    // 'this' is now the window object
    // I must use '_this' to get the class itself
    ...
}

I pass it in to another function

this.deleteRow(this.removeRow);

which in turn calls a jQuery Ajax method, which if successful, invokes the callback like this:

deleteItem(removeRowCallback: (_this:MyClass) => void ): void {
    $.ajax(action, {
        data: { "id": id },
        type: "POST"
    })
    .done(() => {
        removeRowCallback(this);
    })
    .fail(() => {
        alert("There was an error!");
    });
}

The only way I can preserve the ‘this’ reference to my class is to pass it on to the callback, as demonstrated above. It works, but it’s pants code. If I don’t wire up the ‘this’ like this (sorry), then any reference to this in the callback method has reverted to the Window object. Because I’m using arrow functions all the way, I expected that the ‘this’ would be the class itself, as it is elsewhere in my class.

Anyone know how to pass callbacks around in TypeScript, preserving lexical scope?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:27:21+00:00Added an answer on June 17, 2026 at 2:27 pm

    Edit 2014-01-28:

    New readers, make sure you check out Zac’s answer below.

    He has a much neater solution that will let you define and instantiate a scoped function in the class definition using the fat arrow syntax.

    The only thing I will add is that, in regard to option 5 in Zac’s answer, it’s possible to specify the method signature and return type without any repetition using this syntax:

    public myMethod = (prop1: number): string => {
        return 'asdf';
    }
    

    Edit 2013-05-28:

    The syntax for defining a function property type has changed (since TypeScript version 0.8).

    Previously you would define a function type like this:

    class Test {
       removeRow: (): void;
    }
    

    This has now changed to:

    class Test {
       removeRow: () => void;
    }
    

    I have updated my answer below to include this new change.

    As a further aside: If you need to define multiple function signatures for the same function name (e.g. runtime function overloading) then you can use the object map notation (this is used extensively in the jQuery descriptor file):

    class Test {
        removeRow: {
            (): void;
            (param: string): string;
        };
    }
    

    You need to define the signature for removeRow() as a property on your class but assign the implementation in the constructor.

    There are a few different ways you can do this.

    Option 1

    class Test {
    
        // Define the method signature here.
        removeRow: () => void;
    
        constructor (){
            // Implement the method using the fat arrow syntax.
            this.removeRow = () => {
                // Perform your logic to remove the row.
                // Reference `this` as needed.
            }
        }
    
    }
    

    If you want to keep your constructor minimal then you can just keep the removeRow method in the class definition and just assign a proxy function in the constructor:

    Option 2

    class Test {
    
        // Again, define the method signature here.
        removeRowProxy: () => void;
    
        constructor (){
            // Assign the method implementation here.
            this.removeRowProxy = () => {
                this.removeRow.apply(this, arguments);
            }
        }
    
        removeRow(): void {
            // ... removeRow logic here.
        }
    
    }
    

    Option 3

    And finally, if you’re using a library like underscore or jQuery then you can just use their utility method to create the proxy:

    class Test {
    
        // Define the method signature here.
        removeRowProxy: () => void;
    
        constructor (){
            // Use jQuery to bind removeRow to this instance.
            this.removeRowProxy = $.proxy(this.removeRow, this);
        }
    
        removeRow(): void {
            // ... removeRow logic here.
        }
    
    }
    

    Then you can tidy up your deleteItem method a bit:

    // Specify `Function` as the callback type.
    // NOTE: You can define a specific signature if needed.
    deleteItem(removeRowCallback: Function ): void {
        $.ajax(action, {
            data: { "id": id },
            type: "POST"
        })
    
        // Pass the callback here.
        // 
        // You don't need the fat arrow syntax here
        // because the callback has already been bound
        // to the correct scope.
        .done(removeRowCallback)
    
        .fail(() => {
            alert("There was an error!");
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TypeScript class definition that starts like this; module Entities { export
I have a function defined in a object like this: connect(callback?: (connected: bool) =>
I have this typescript code: module MyPage { export class MyVm { ToDo :
I have this code: function setSidebar(visible) { use strict if (visible === show) {
How do I use custom events in TypeScript? In jQuery I do this: $(#btnShowExplorer).click(function
have written this little class, which generates a UUID every time an object of
I am trying to use TypeScript in a Windows 8 app (html5/JS) I have
I have a node app that has a string of require s, like this:
I have a webview that performs a search, this code works great for below
My TypeScript application makes frequent use of a pattern where I have a set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.