../courses.php
TypeScript
TS105
3
Function Inputs Must Be Clear
Typed function parameters make bad calls easier to catch before runtime.
Why should function inputs have types?
Function input types explain what values a function expects and let TypeScript reject calls that pass the wrong kind of data.
Functions often fail because they receive the wrong input.
A price function should receive a number, not text.
A greeting function should receive a string, not an object.
TypeScript lets you write these expectations directly on the function.
That turns the function signature into useful documentation and a safety check.
Typed function input and output
function addTax(price: number): number {
return price * 1.05;
}
const total = addTax(100);
console.log(total);
price: number means the function expects a number input.
: number after the parameter list means the function returns a number.
addTax(100) is a valid call.
Calling the function with text would be rejected.
The function signature explains how to use the function correctly.
Inputs
Typed parameters show what values are allowed.
Outputs
Return types show what the function gives back.
Bad calls
Wrong function calls are easier to catch.
Documentation
The function explains itself more clearly.
Wrong function input
Correct function input
Ask ChatGPT: "Check this TypeScript function signature. What inputs and output does it promise?"
Create a function with a typed number parameter.
Add a typed return value.
Call the function correctly.
Call the function incorrectly.
Explain why TypeScript rejects the bad call.
Leaving important function inputs unclear.
Passing strings where numbers are expected.
Ignoring return types.
Using any to avoid understanding the input.
Thinking the function name is enough documentation.
Use typed function parameters and return types to make function calls safer.
Login - Jit4All
Login
Welcome back to Jit4All