TypeScript Tutorial #3 - Type Annotations Around Functions

Pubblicato il: 17 giugno 2021
sul canale di: Free Tutorial Wale
15
0

#Function

Functions are the fundamental building block of any application in JavaScript. They’re how you build up layers of abstraction, mimicking classes, information hiding, and modules. In TypeScript, while there are classes, namespaces, and modules, functions still play the key role in describing how to do things. TypeScript also adds some new capabilities to the standard JavaScript functions to make them easier to work with.

To begin, just as in JavaScript, TypeScript functions can be created both as a named function or as an anonymous function. This allows you to choose the most appropriate approach for your application, whether you’re building a list of functions in an API or a one-off function to hand off to another function.

function add(x: number, y: number): number {
return x + y;
}

let myAdd = function (x: number, y: number): number {
return x + y;
};

In TypeScript, every parameter is assumed to be required by the function. This doesn’t mean that it can’t be given null or undefined, but rather, when the function is called, the compiler will check that the user has provided a value for each parameter. The compiler also assumes that these parameters are the only parameters that will be passed to the function. In short, the number of arguments given to a function has to match the number of parameters the function expects.


In questa pagina del sito puoi guardare il video online TypeScript Tutorial #3 - Type Annotations Around Functions della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Free Tutorial Wale 17 giugno 2021, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 15 volte e gli è piaciuto 0 spettatori. Buona visione!