

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Function Type Syntax
incomplete
2: Inferred Return Types
incomplete
3: Void
incomplete
4: Function Types
incomplete
5: Type Alias
incomplete
6: Importing Types
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
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.
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.