Skip to content
tezvyn:
Menu

TypeScript Function Types: Expressions vs. Signatures

Source: typescriptlang.orgMediumHow cards are made

TypeScript describes function shapes with type expressions or call signatures. Use a simple expression like (s: string) => void for callbacks. For functions with properties, use a call signature. The footgun: parameter names are required in type expressions.

Why it exists

In JavaScript and TypeScript, functions are first-class citizens—they are values that can be passed to other functions, returned from them, and assigned to variables. To maintain type safety, TypeScript needs a way to describe the "shape" of these function values: what arguments they expect and what they return.

The mental model

Think of a function type as a contract for a callable value. It defines the function's signature, including its parameters and return type. TypeScript offers different syntaxes for this contract depending on whether the function is a simple callback, a callable object with properties, or a class constructor.

How it works

TypeScript provides three main ways to describe a function's type.

First, the Function Type Expression: (a: string) => void. This syntax looks like an arrow function and is the simplest way to type a function. It's ideal for callbacks and variables holding a function. The parameter name (a in this case) is required. Writing (string) => void is a common mistake; it defines a function with a parameter named string of type any.

Second, the Call Signature: { (someArg: number): boolean; description: string; }. This is written inside an object type. It's for describing a value that is callable but also has its own properties. Note the use of a colon : instead of an arrow => to separate the parameters from the return type.

Third, the Construct Signature: { new (s: string): SomeObject; }. By adding the new keyword, you describe a function that can be used as a constructor (i.e., called with new). This can be combined with a call signature for objects like JavaScript's Date that can be invoked with or without new.

When to use it

Use a function type expression for the majority of cases, like defining the type for a callback or a simple function variable. Use a call signature when you need to model a function that also has static-like properties, such as a utility function with a version property. Use a construct signature when typing class constructors or factory functions.

When not to use it

Avoid using a function type expression if you need to describe properties on the function itself; a call signature is the correct tool. Conversely, don't use the more verbose call signature syntax when a simple function type expression is sufficient.

One canonical example

To describe a function that is callable and has a description property, you use a call signature:

type DescribableFunction = {

description: string; (someArg: number): boolean; };

function doSomething(fn: DescribableFunction) {
console.log(fn.description + " returned " + fn(6));
}

This allows you to pass a function that satisfies both conditions: it's callable with a number and returns a boolean, and it has a string property named description.

Interview question

When should a TypeScript call signature be used instead of a function type expression?

  • a.When defining a function type that can also be used as a class constructor.
  • b.When you need to define a function type without explicitly naming its parameters.
  • c.When the function's return type needs to be separated from its parameters using a colon (:) instead of an arrow (=>).
  • d.When the function type needs to specify properties that the function itself possesses.Correct
Why?

The card states that a call signature is used "for describing a value that is callable but also has its own properties." Function type expressions are for simple callbacks without properties. Option C describes a syntactic difference, not the functional reason for choosing one over the other.

Just read this? Test yourself on what you have been reading.

Read the original → typescriptlang.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on typescript — each one lists the topics its interview covers.

See open roles