Is it possible to create an object / class / interface that represents a signature of a function?

3

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

What I have today:

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

The object expression 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{

}

or

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

}

or

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

For my method to be something like:

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

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

Is there anything I can do in this regard?

  

See # (% 2)% 20% 3A% 22% 3B% 0D% 0A% 0D% 0Adocument.body.appendChild (h1)% 3B " the ability to use TypeScript Arrow function > as expressions

asked by anonymous 18.07.2017 / 14:17

1 answer

2

You can use type aliases :

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

Thus signing the method:

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

#

  

Based on the answer to this question in SOen .

    
18.07.2017 / 15:16