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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 1

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

bookAnnotating Function Parameters

Scorri per mostrare il menu

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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 1
some-alt