typescript – Is it possible to create an object/class/interface that represents a function's signature?

Question:

Everything already works as expected in my code. What I'm trying to do is make it more verbose, making the method signatures more self-explanatory (I know I can use Doc comments for that, but I'd also like to use TypeScript types) and better validated by TSLint 's of life.

what i have today:

class Test{
    testMetadada<T>(expression: (t: T) => void) {
        // ...
    }
}

The expression object is of type (t: T) => void , which is not very explanatory, I would like, for example, that something like:

class Expression<T> extends (t: T) => void{

}

Where

interface Expression<T> extends (t: T) => void{

}

Where

let Expression = ((t: T) => void)<T>;

So my method was something like:

class Test{
    testMetadada<T>(expression: Expression) {
        // ...
    }
}

Where Expression represents the same function (t: T) => void .

Is there anything you can do about it?

Here's an example of what I'm trying to implement with this (the ability to use TypeScript's Arrow function as C# Lambda Expressions to get metadata)

Answer:

You can use type aliases :

type Expression<T> = (t: T) => void;

Thus, the method signature is:

class Test{
    testMetadada<T>(expression: Expression<T>) {
        // ...
    }
}

Updated example of the solution.


Based on the answer to that question on SOen .

Scroll to Top