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

  • Home
  • SEARCH
  • 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 9219347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:05:44+00:00 2026-06-18T03:05:44+00:00

In TypeScript, I can declare a parameter of a function as a type Function.

  • 0

In TypeScript, I can declare a parameter of a function as a type Function. Is there a “type-safe” way of doing this that I am missing? For example, consider this:

class Foo {
    save(callback: Function) : void {
        //Do the save
        var result : number = 42; //We get a number from the save operation
        //Can I at compile-time ensure the callback accepts a single parameter of type number somehow?
        callback(result);
    }
}

var foo = new Foo();
var callback = (result: string) : void => {
    alert(result);
}
foo.save(callback);

The save callback is not type safe, I am giving it a callback function where the function’s parameter is a string but I am passing it a number, and compiles with no errors. Can I make the result parameter in save a type-safe function?

TL;DR version: is there an equivalent of a .NET delegate in TypeScript?

  • 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-18T03:05:45+00:00Added an answer on June 18, 2026 at 3:05 am

    Sure. A function’s type consists of the types of its argument and its return type. Here we specify that the callback parameter’s type must be “function that accepts a number and returns type any“:

    class Foo {
        save(callback: (n: number) => any) : void {
            callback(42);
        }
    }
    var foo = new Foo();
    
    var strCallback = (result: string) : void => {
        alert(result);
    }
    var numCallback = (result: number) : void => {
        alert(result.toString());
    }
    
    foo.save(strCallback); // not OK
    foo.save(numCallback); // OK
    

    If you want, you can define a type alias to encapsulate this:

    type NumberCallback = (n: number) => any;
    
    class Foo {
        // Equivalent
        save(callback: NumberCallback) : void {
            callback(42);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to use TypeScript to declare functions with parameters that can be either
Given the fact that TypeScript is Open Source, is this Web-Based IDE also available
This question is the direct analogon to Class type check in TypeScript I need
Can someone explain this? I realize the jQuery interface is the one that comes
What is the TypeScript language? What can it do that JavaScript or available libraries
Can someone explain how would this look in TypeScript. Can it be done via
UPDATE - the context of this question was pre-TypeScript 1.4. Since that version, my
In JavaScript I can do this: function f() {} f.prop = property; I want
I was wondering how could one declare a typed-function array in TypeScript. For instance,
In typescript, I can write something like this: $('#something').fadeOut(400, (): void => { this.invokeAnotherMethod();

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.