Function Type Expressions
What you'll learn: How to create reusable type aliases for function signatures that you can use across your codebase.
The Core Idea
You've already seen how to write function types inline when annotating variables or parameters. But what if you need to use the same function signature in multiple places? Function type expressions let you define a function's shape once and reuse it everywhere through a type alias.
Think of it like defining a blueprint. Instead of describing what a hammer looks like every time you need one, you create a "Hammer" specification once and refer to it by name whenever you need that tool.
Syntax
You create a function type expression using the type keyword followed by a name and the function signature:
type GreetFunction = (name: string) => string;
Now GreetFunction is a reusable type you can apply anywhere:
const greet: GreetFunction = (name) => {
return `Hello, ${name}!`;
};
const greetLoudly: GreetFunction = (name) => {
return `HELLO, ${name.toUpperCase()}!`;
};
Notice how the parameter name in both functions automatically inherits the string type from the type alias—you don't need to annotate it again.
Why This Matters
Function type expressions make your code more maintainable. If your function signature needs to change, you update it once in the type alias, and all functions using that type automatically comply. This is especially valuable when passing functions as parameters or storing them in objects.
Key Takeaway: Function type expressions let you define reusable function signatures with type, making your code cleaner and easier to maintain when the same function shape appears multiple times.