Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Typing Arrow Functions | Typing Functions in TypeScript
TypeScript Functions and Parameters

bookTyping Arrow Functions

Arrow functions in TypeScript offer a concise way to write functions, especially for simple operations and callbacks. The syntax for arrow functions uses the => symbol, allowing you to define anonymous functions with a shorter syntax than traditional function expressions. When working in TypeScript, you can add type annotations to arrow function parameters and return values to ensure type safety and clarity in your code.

123456789101112
// An arrow function with typed parameters and a typed return value const add = (a: number, b: number): number => a + b; // Valid calls console.log(add(2, 3)); // 5 console.log(add(10, -4)); // 6 console.log(add(2.5, 3.5)); // 6 // Invalid (failure) calls // console.log(add("2", "3")); // Error: Argument of type 'string' is not assignable to parameter of type 'number'. // console.log(add(true, 5)); // Error: Argument of type 'boolean' is not assignable to parameter of type 'number'. // console.log(add(2)); // Error: Expected 2 arguments, but got 1.
copy

TypeScript has powerful type inference, so you do not always need to explicitly annotate parameter types or return types in arrow functions. If TypeScript can determine the types from context, it will infer them for you. However, explicit typing is necessary when the context does not provide enough information, or when you want to make your code more readable and maintainable. Adding explicit types can help prevent bugs and make your intentions clear, especially in complex functions or when sharing code with others.

question mark

Which arrow function correctly adds explicit type annotations for both parameters and the return value in TypeScript?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 4

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 9.09

bookTyping Arrow Functions

Pyyhkäise näyttääksesi valikon

Arrow functions in TypeScript offer a concise way to write functions, especially for simple operations and callbacks. The syntax for arrow functions uses the => symbol, allowing you to define anonymous functions with a shorter syntax than traditional function expressions. When working in TypeScript, you can add type annotations to arrow function parameters and return values to ensure type safety and clarity in your code.

123456789101112
// An arrow function with typed parameters and a typed return value const add = (a: number, b: number): number => a + b; // Valid calls console.log(add(2, 3)); // 5 console.log(add(10, -4)); // 6 console.log(add(2.5, 3.5)); // 6 // Invalid (failure) calls // console.log(add("2", "3")); // Error: Argument of type 'string' is not assignable to parameter of type 'number'. // console.log(add(true, 5)); // Error: Argument of type 'boolean' is not assignable to parameter of type 'number'. // console.log(add(2)); // Error: Expected 2 arguments, but got 1.
copy

TypeScript has powerful type inference, so you do not always need to explicitly annotate parameter types or return types in arrow functions. If TypeScript can determine the types from context, it will infer them for you. However, explicit typing is necessary when the context does not provide enough information, or when you want to make your code more readable and maintainable. Adding explicit types can help prevent bugs and make your intentions clear, especially in complex functions or when sharing code with others.

question mark

Which arrow function correctly adds explicit type annotations for both parameters and the return value in TypeScript?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 4
some-alt