Annotating 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.
123456789101112131415function 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.
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.
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
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
Annotating Function Parameters
Desliza para mostrar el menú
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.
123456789101112131415function 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.
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.
¡Gracias por tus comentarios!