Every piece of JavaScript code is also considered valid TypeScript code, ensuring that your function will run in TypeScript as intended.
Values can be passed just like in a typical JavaScript function:
window.X = function(numberToAdd) {
return numberToAdd + 5
}
TypeScript offers the ability to specify types for both the input values and the return value of the function. For example, adding a type to an input value would look like this:
window.X = function(numberToAdd: number) {
return numberToAdd + 5
}
If you were to try passing a string to this function, TypeScript would catch it as an error.
You can also add a return type to the function, like so:
window.X = function(numberToAdd): number {
return numberToAdd + 5
}
If you were to then try accessing properties that are not applicable to a number (such as the property "length"), TypeScript would alert you to the error.
These TypeScript errors provide valuable insight into potential mistakes in your code before execution, helping to prevent bugs down the line.