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

bookAnnotating Function Parameters

When you define functions in TypeScript, you can specify the types of each parameter. This is important because it allows TypeScript to check that the correct types of values are being passed to your functions, which helps prevent runtime errors and makes your code easier to understand and maintain. By explicitly typing function parameters, you provide clear expectations for anyone using your functions and enable better tooling support, such as autocompletion and inline documentation.

123456789101112131415
function add(a: number, b: number): number { return a + b; } // Valid calls console.log(add(5, 3)); // 8 console.log(add(10, 15)); // 25 console.log(add(-4, 9)); // 5 console.log(add(2.5, 3.5)); // 6 // Invalid (failure) calls // console.log(add("5", "3")); // Error: Argument of type 'string' is not assignable to parameter of type 'number'. // console.log(add(true, false)); // Error: Argument of type 'boolean' is not assignable to parameter of type 'number'. // console.log(add(5)); // Error: Expected 2 arguments, but got 1. // console.log(add(5, 3, 2)); // Error: Expected 2 arguments, but got 3.
copy

In the add function above, both parameters a and b are annotated with the number type. This means that TypeScript will enforce that only numbers can be passed as arguments to this function. The syntax a: number and b: number specifies the expected type for each parameter. If you try to call add("2", 3) or add(2, "3"), TypeScript will generate an error, alerting you that the provided arguments do not match the expected types. This strict type checking helps you catch mistakes early and ensures that your functions behave as intended.

question mark

What are the main benefits of annotating function parameters with types in TypeScript?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 1

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you explain how to specify optional parameters in TypeScript functions?

What happens if I want to allow different types for a parameter?

Can you show how to define a function that returns a different type?

Awesome!

Completion rate improved to 9.09

bookAnnotating Function Parameters

Veeg om het menu te tonen

When you define functions in TypeScript, you can specify the types of each parameter. This is important because it allows TypeScript to check that the correct types of values are being passed to your functions, which helps prevent runtime errors and makes your code easier to understand and maintain. By explicitly typing function parameters, you provide clear expectations for anyone using your functions and enable better tooling support, such as autocompletion and inline documentation.

123456789101112131415
function add(a: number, b: number): number { return a + b; } // Valid calls console.log(add(5, 3)); // 8 console.log(add(10, 15)); // 25 console.log(add(-4, 9)); // 5 console.log(add(2.5, 3.5)); // 6 // Invalid (failure) calls // console.log(add("5", "3")); // Error: Argument of type 'string' is not assignable to parameter of type 'number'. // console.log(add(true, false)); // Error: Argument of type 'boolean' is not assignable to parameter of type 'number'. // console.log(add(5)); // Error: Expected 2 arguments, but got 1. // console.log(add(5, 3, 2)); // Error: Expected 2 arguments, but got 3.
copy

In the add function above, both parameters a and b are annotated with the number type. This means that TypeScript will enforce that only numbers can be passed as arguments to this function. The syntax a: number and b: number specifies the expected type for each parameter. If you try to call add("2", 3) or add(2, "3"), TypeScript will generate an error, alerting you that the provided arguments do not match the expected types. This strict type checking helps you catch mistakes early and ensures that your functions behave as intended.

question mark

What are the main benefits of annotating function parameters with types in TypeScript?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 1
some-alt