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

bookTyping Function Return Values

When working with functions in TypeScript, it is important to understand how to specify the type of value a function returns. This is called a return type annotation. By explicitly stating the return type, you help TypeScript catch errors early and make your code more predictable. To annotate a function's return type, you place a colon and the type right after the parameter list and before the function body. You should use return type annotations when you want to make your code clearer, enforce a specific return type, or when TypeScript's inference might not be strict enough for your needs.

1234567891011121314
function greet(name: string): string { return "Hello, " + name + "!"; } // Valid calls console.log(greet("Alice")); // "Hello, Alice!" console.log(greet("Bob")); // "Hello, Bob!" console.log(greet("TypeScript")); // "Hello, TypeScript!" // Invalid (failure) calls // console.log(greet()); // Error: Expected 1 argument, but got 0. // console.log(greet(42)); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. // console.log(greet(true)); // Error: Argument of type 'boolean' is not assignable to parameter of type 'string'. // console.log(greet(["Alice"])); // Error: Argument of type 'string[]' is not assignable to parameter of type 'string'.
copy

TypeScript is often able to infer the return type of a function based on the value you return. For simple functions, you may not need to specify the return type explicitly. However, providing an explicit return type can be helpful in more complex scenarios, such as when a function contains multiple return statements or when you want to ensure the function always returns a specific type. Explicit return type annotations can also make your code more readable and maintainable, especially in larger codebases where the function's intent might not be immediately clear.

question mark

What is the main benefit of adding a return type annotation to a TypeScript function?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 2

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 9.09

bookTyping Function Return Values

Scorri per mostrare il menu

When working with functions in TypeScript, it is important to understand how to specify the type of value a function returns. This is called a return type annotation. By explicitly stating the return type, you help TypeScript catch errors early and make your code more predictable. To annotate a function's return type, you place a colon and the type right after the parameter list and before the function body. You should use return type annotations when you want to make your code clearer, enforce a specific return type, or when TypeScript's inference might not be strict enough for your needs.

1234567891011121314
function greet(name: string): string { return "Hello, " + name + "!"; } // Valid calls console.log(greet("Alice")); // "Hello, Alice!" console.log(greet("Bob")); // "Hello, Bob!" console.log(greet("TypeScript")); // "Hello, TypeScript!" // Invalid (failure) calls // console.log(greet()); // Error: Expected 1 argument, but got 0. // console.log(greet(42)); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. // console.log(greet(true)); // Error: Argument of type 'boolean' is not assignable to parameter of type 'string'. // console.log(greet(["Alice"])); // Error: Argument of type 'string[]' is not assignable to parameter of type 'string'.
copy

TypeScript is often able to infer the return type of a function based on the value you return. For simple functions, you may not need to specify the return type explicitly. However, providing an explicit return type can be helpful in more complex scenarios, such as when a function contains multiple return statements or when you want to ensure the function always returns a specific type. Explicit return type annotations can also make your code more readable and maintainable, especially in larger codebases where the function's intent might not be immediately clear.

question mark

What is the main benefit of adding a return type annotation to a TypeScript function?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 2
some-alt