We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Type Alias

It can get really cumbersome to write out long custom types whenever you want to use them. For example, maybe we have a function that accepts another function as input. Let's use a totally make-believe example, something that sets a timeout:

function setLoggerTimeout(
  loggerCallback: (s1: string, s2: string) => string,
  delay: number,
) {
  // do something
}

That's a nasty function signature... let's use the type keyword instead to create a type alias:

type LoggerCallback = (s1: string, s2: string) => string;

Now anytime we need to use this specific kind of function (one that accepts two strings and returns a string), we can just use LoggerCallback:

function setLoggerTimeout(loggerCallback: LoggerCallback, delay: number) {
  // do something
}

Muuuuuch better! It's easy to read and reusable! Why is that important? It's less prone to copying errors as we use it in other places in our code. And in the future, if we want to change it, we only have to modify the type declaration rather than everywhere it's used.

Assignment

Support.ai's internal code style guide requires functions to be clearly typed when used as higher-order functions.

A function type alias describes a function's shape; it doesn't declare or create functions. You don't need to explicitly use the alias in your function declarations – as long as the signature matches, the function "conforms to" the type. The tests will import and use your SupportResponse type to verify that your functions have the correct signature before runtime.