
Function Types
One of the neat things about JavaScript is that functions can be assigned to variables.
let myFavoriteFunction = console.log // Note the lack of parentheses.
myFavoriteFunction('Hello World') // Prints: Hello World
One of the neat things about TypeScript is that we can precisely control the kinds of functions assignable to a variable. We do this using function types, which specify the argument types and return type of a function. Here’s an example of a function type that is only compatible with functions that take in two string arguments and return a number.
type StringsToNumberFunction = (arg0: string, arg1: string) => number
This syntax is just like arrow notation for functions, except instead of the return value we put the return type. In this case, the return type is number. Because this is just a type, we did not write the function body at all. A variable of type StringsToNumberFunction can be assigned any compatible function:
let myFunc: StringsToNumberFunction
myFunc = function (firstName: string, lastName: string) {
return firstName.length + lastName.length
}
myFunc = function (whatever: string, blah: string) {
return whatever.length - blah.length
}
// Neither of these assignments results in a type error.
As we can see above, it doesn’t matter what we name the function parameters, so long as they have the correct types (string and string). Therefore, it doesn’t matter what we name the parameters in the type annotation (above, we chose arg0 and arg1).
There’s something important to remember here. We must never be tempted to omit the parameter names or the parentheses around the parameters in a function type annotation, even if there is only one parameter. This code will not run!
type StringToNumberFunction = (string)=>number; // NO
type StringToNumberFunction = arg: string=>number; // NO NO NO NO
Function types are most useful when applied to callback functions. With how common [callback functions](callback functions) are, it’s useful to know how to type them appropriately. Let’s get some practice using function types with callback functions!
Related articles
10 Essential Steps for Migrating Your JavaScript Project to TypeScript in 2025
Discover the practical roadmap to seamlessly transition your JavaScript projects to TypeScript, enhancing type safety and developer productivity in 2025.
Crafting Dynamic Data-Driven User Interfaces with React and TypeScript
Learn how to effortlessly craft dynamic, data-driven user interfaces using React and TypeScript, enhancing your app's responsiveness and user experience.
Mastering Next-Gen API Security: JWTs with Node.js & TypeScript in 2025
Learn step-by-step how to enhance your API’s security using JWTs with Node.js and TypeScript, preparing your applications for the security demands of 2025.