Can we define delegate (function) types in TypeScript?

Topics: Language Specification
Nov 22, 2012 at 2:25 PM

Can we define delegate (function) types in TypeScript?

Nov 22, 2012 at 4:57 PM

var fn: (x: number) => number;

fn = (x). => x*x;

 

Or same but named:

interface NumericalFunction {

   (x: number): number;

Nov 22, 2012 at 5:00 PM
Edited Nov 22, 2012 at 5:01 PM

The important thing to ask though is not if I can, but whether I should. Who is going to benefit from that function? Am doing a good service to the human race by abstracting those functions away?

Those are difficult questions we should concentrate on, not technicalities like syntax or paradigms. 

Feb 1, 2013 at 7:43 PM
the C# team thought that it would be ok to abstract a function type into DELEGATE.
Why does Typescript not have the need to.

if my API needs to pass around some functions with more complex signatures, repeating the type description each time is error prone and time consuming,

why not:

delegate EventHandler( sender : object, args : EventArgs );

...

class A
{
           ....
          public void Register( handler : EventHandler )
...
}
Developer
Feb 2, 2013 at 10:33 PM
It's already supported:
interface EventHandler {
    (sender: Object; args: EventArgs): void;
}

function Register(handler: EventHandler) { ... }
A named function type is simply an interface with one or more call signatures.
Feb 2, 2013 at 10:40 PM
<div dir="ltr">Thanks. Interesting and not obvious. Is this in the docs?</div> <div><br> <br> <div>On Sun, Feb 3, 2013 at 1:33 AM, ahejlsberg <span dir="ltr">&lt;<a href="mailto:notifications@codeplex.com" target="_blank">notifications@codeplex.com</a>&gt;</span> wrote:<br> <blockquote style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex"> <div> <p>From: ahejlsberg</p> <div>It's already supported: ``` interface EventHandler { (sender: Object; args: EventArgs): void; } function Register(handler: EventHandler) { ... } ``` A named function type is simply an interface with one or more call signatures.</div> <div> <p>Read the <a href="http://typescript.codeplex.com/discussions/404243#post995669" target="_blank"> full discussion online</a>.</p> <p>To add a post to this discussion, reply to this email (<a href="mailto:typescript@discussions.codeplex.com?subject=[typescript:404243]" target="_blank">typescript@discussions.codeplex.com</a>)</p> <p>To start a new discussion for this project, email <a href="mailto:typescript@discussions.codeplex.com" target="_blank"> typescript@discussions.codeplex.com</a></p> <p>You are receiving this email because you subscribed to this discussion on CodePlex. You can <a href="https://typescript.codeplex.com/discussions/404243/unsubscribe/" target="_blank"> unsubscribe</a> on CodePlex.com.</p> <p>Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com</p> </div> </div> </blockquote> </div> <br> </div>
Mar 15, 2013 at 3:20 AM
function references in C# are called delegates. Function references in Javascript and therefore Typescript are just normal variables which have a call signature. Call signatures are mentioned on page 24 , section 3.5.3.1 of the typescript language specification : http://www.typescriptlang.org/Content/TypeScript%20Language%20Specification.pdf