Reputation: 793
Given this type definition
type MyFunction = (prop: string) => boolean
I know it's possible to type an anonymous function
const myFunction: MyFunction = (prop) => true
But I'm not sure on how to do this for a regular function definition
function myFunction (prop) {
return true;
}
Is this possible, and if so how? Thanks
EDIT
Just to clarify, I'm looking for a way to type the function using the same type definition, and not broken down inside the function, if it's at all possible. The main reason is to be able to re-use the type definition in other places.. callbacks, declaration files etc
Upvotes: 2
Views: 71
Reputation: 3527
Like this
function myFunction (prop: string) : boolean {
return true;
}
EDIT
If you want to make a reusable function type, then you have to create your functions as variables, i.e. const myFunc: FuncType = function(param: string) : boolean { ... }
. You can't do that with declarative function ()
syntax without creating a variable.
Upvotes: 2