Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 5

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 7.14

bookTyping Functions with Parameters and Returns

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 5
some-alt