Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте 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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 4

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Awesome!

Completion rate improved to 9.09

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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 4
some-alt