#
TypeScript Functions | webally.co.za
By
TypeScript
is a bit more strict about how you declare functions and what params you send the function and what it returns
Here is a function that does some kind of calculation
#
Unused Parameters
function calc(income: number): number {
return 0;
}
The above function
has an unused param
, I don't like to have any unused params
so I like to change the following setting in jsconfig.json
"noUnusedParameters": true,
#
Implicit Returns
Also switch on the following settings:
"noImplicitReturns": true, // When the function does not always return a value
"noUnusedLocals": true // when there are local variables declared inside a function but they are never used
#
Default values for parameters
function calc(income = 0): number {
return income * 2;
}
#
Optional value for parameters
Below the tax param is optional because of the ?
before the :
at the second param
function calc(income = 0, tax?: number): number {
tax = tax || 14;
return income * tax;
}