OK, this is the most ugly way to achieve it, you can simply declare a variable as
any type, then later, you can assign a function to it. It’s not recommend.
Sometimes, when you design the interfaces, you have no idea what the actual signature will be, but instead declaring a ‘any’ type, you can use a keyword ‘Function’, so you can take advantage of the type checking later.
OK. So now when you want assign a value other than function to this variable ‘a’, the compiler will throw an error :
Type ‘xxxxx’ is not assignable to type ‘Function’.
Now, as your projects goes on, you have whole idea of what’s going on there. So you can go back and modify your declaration to a more precise version using the fancy arrow function syntax, feel free to use it, it marked as ‘standard’ in ECMA2015.
The syntax here is very simple, it is just like the lambda function in Java or C#, the ‘string’ after the arrow is the type of the return value. The para: string defines the type of the parameter. It takes a
string and return a
We can use
type to declare a function type:
We are Typescript, we’d love to use the beloved
interface for everything, well, you can, for just a function.
In practice, I often use
Solution 4 - Type. Maybe just because it looks simpler than