Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Typing Functions with Parameters and Returns | Transitioning from JavaScript to TypeScript
TypeScript for JavaScript Developers

bookTyping Functions with Parameters and Returns

Adding type annotations to your functions is a key step when moving from JavaScript to TypeScript. In JavaScript, functions accept any type of argument, and their return values can be unpredictable. TypeScript allows you to specify exactly what types of parameters a function expects and what type it will return. This not only helps prevent bugs but also improves code readability and maintainability.

To annotate function parameters, you simply add a colon and the desired type after each parameter name. You can also specify the return type of a function by adding a colon and the type after the parameter list. If a parameter is optional, append a question mark to the parameter name. For parameters with default values, TypeScript will infer the type from the default unless you specify it explicitly. These features together make your functions safer and more self-documenting.

1234567891011121314
// JavaScript: Function with dynamic parameters function add(a, b) { return a + b; } // TypeScript: Typed parameters and return type function addTyped(a: number, b: number): number { return a + b; } // TypeScript: Optional and default parameters function greet(name: string, greeting?: string, punctuation: string = "!"): string { return (greeting || "Hello") + ", " + name + punctuation; }
copy
12345678
// TypeScript: Typing a function that processes user input and returns a formatted string function formatUserInput(username: string, age: number): string { return `User ${username} is ${age} years old.`; } // Usage const message = formatUserInput("Alice", 30); // "User Alice is 30 years old."
copy
Note
Definition

A function with a return type of void indicates that it does not return any value. In contrast, a return type of undefined means the function may explicitly return the value undefined. While both suggest the absence of a meaningful result, void is used for functions where returning a value is not intended, whereas undefined can be a specific, intentional return value.

1. What is the benefit of annotating function parameters in TypeScript?

2. What does a function with a return type of void indicate?

question mark

What is the benefit of annotating function parameters in TypeScript?

Select the correct answer

question mark

What does a function with a return type of void indicate?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 5

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Awesome!

Completion rate improved to 7.14

bookTyping Functions with Parameters and Returns

Swipe um das Menü anzuzeigen

Adding type annotations to your functions is a key step when moving from JavaScript to TypeScript. In JavaScript, functions accept any type of argument, and their return values can be unpredictable. TypeScript allows you to specify exactly what types of parameters a function expects and what type it will return. This not only helps prevent bugs but also improves code readability and maintainability.

To annotate function parameters, you simply add a colon and the desired type after each parameter name. You can also specify the return type of a function by adding a colon and the type after the parameter list. If a parameter is optional, append a question mark to the parameter name. For parameters with default values, TypeScript will infer the type from the default unless you specify it explicitly. These features together make your functions safer and more self-documenting.

1234567891011121314
// JavaScript: Function with dynamic parameters function add(a, b) { return a + b; } // TypeScript: Typed parameters and return type function addTyped(a: number, b: number): number { return a + b; } // TypeScript: Optional and default parameters function greet(name: string, greeting?: string, punctuation: string = "!"): string { return (greeting || "Hello") + ", " + name + punctuation; }
copy
12345678
// TypeScript: Typing a function that processes user input and returns a formatted string function formatUserInput(username: string, age: number): string { return `User ${username} is ${age} years old.`; } // Usage const message = formatUserInput("Alice", 30); // "User Alice is 30 years old."
copy
Note
Definition

A function with a return type of void indicates that it does not return any value. In contrast, a return type of undefined means the function may explicitly return the value undefined. While both suggest the absence of a meaningful result, void is used for functions where returning a value is not intended, whereas undefined can be a specific, intentional return value.

1. What is the benefit of annotating function parameters in TypeScript?

2. What does a function with a return type of void indicate?

question mark

What is the benefit of annotating function parameters in TypeScript?

Select the correct answer

question mark

What does a function with a return type of void indicate?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 5
some-alt