Callable
You can annotate callables as a part of a type or an interface as follows
An instance of such an interface would be a function that returns a string e.g.
Obvious examples
Of course such a callable annotation can also specify any arguments / optional arguments / rest arguments as needed. e.g. here is a complex example:
An interface can provide multiple callable annotations to specify function overloading. For example:
Of course, like the body of any interface, you can use the body of a callable interface as a type annotation for a variable. For example:
Arrow Syntax
To make it easy to specify callable signatures, TypeScript also allows simple arrow type annotations. For example, a function that takes a number
and returns a string
can be annotated as:
Only limitation of the arrow syntax: You can't specify overloads. For overloads you must use the full bodied
{ (someArgs): someReturn }
syntax.
Newable
Newable is just a special type of callable type annotation with the prefix new
. It simply means that you need to invoke with new
e.g.
Last updated