Typing Arrow Functions
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 23 of 73.
For arrow functions, you add type annotations to parameters within the parentheses, just like regular functions. The return type annotation comes after the parameter list but before the arrow (=>):
const multiply = (a: number, b: number): number => {
return a * b;
};
const greet = (name: string): string => `Hello, ${name}!`;The key difference from regular function declarations is that the return type annotation appears before the arrow rather than after the function name. This maintains TypeScript's type safety while preserving the clean, modern syntax that makes arrow functions popular.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyConvert the following regular function declaration to an arrow function while maintaining all type annotations:
function subtract(a: number, b: number): number {
return a - b;
}Create an arrow function named subtract that performs the same operation with the same parameter types and return type.
Then create two additional arrow functions:
- An arrow function named
createMessagethat takes a parametertextof typestringand returns a string with the format"Message: [text]". Include explicit type annotations for both the parameter and return type. - An arrow function named
isPositivethat takes a parameternumof typenumberand returnstrueif the number is greater than 0, otherwisefalse. Include explicit type annotations for both the parameter and return type.
Test your arrow functions by calling them with the following values and printing the results:
- Call
subtractwith10and3 - Call
createMessagewith"Hello World" - Call
isPositivewith-5 - Call
isPositivewith8
Print each result on a separate line in the order specified above.
Cheat sheet
Arrow functions use type annotations for parameters within parentheses, with the return type annotation before the arrow (=>):
const multiply = (a: number, b: number): number => {
return a * b;
};
const greet = (name: string): string => `Hello, ${name}!`;The return type annotation appears before the arrow rather than after the function name, maintaining TypeScript's type safety with arrow function syntax.
Try it yourself
// TODO: Convert the regular function to arrow function and create the additional arrow functions
// TODO: Create the subtract arrow function with type annotations
// TODO: Create the createMessage arrow function with type annotations
// TODO: Create the isPositive arrow function with type annotations
// TODO: Test your arrow functions and print the resultsThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To TypeScript
1Getting Started with TS
What is TypeScript?Why Use TypeScript?Your First TypeScript CodeCompilation Process & ErrorsRecap: Introduction to TS4Working with Functions
Typing Params & Return ValuesTyping Arrow FunctionsThe 'void' Return TypeOptional Parameters with '?'Default Parameter ValuesTyping Rest ParametersDefining Function TypesRecap: Building Typed Funcs2Core Types
Basic Types: str, num, booleanThe 'any' Type: Escape HatchThe 'unknown' TypeWorking with 'null' & 'undef'Type Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Types: Aliases, Unions & Inter
Type Aliases for PrimitivesUnion Types ('|')Working with Union TypesLiteral TypesIntersection Types ('&')Combining Type AliasesRecap: Advanced Type Combos3Data Structure: Arrays & Tuple
Typed Arrays'readonly' Modifier for ArraysWhat is a Tuple?Declaring and Accessing TuplesDestructuring TuplesReadonly TuplesMulti-dimensional Typed Arrays Spread Operator with ArraysRecap: Arrays and Tuples