← Trail· Lesson 6 of 10
See this lesson in JavaScript →
Functions
Write typed signatures: parameters, return types and optional parameters.

A function groups instructions for a task. In TypeScript, its signature is a contract: function dobro(numero: number): number. Calling dobro('6') becomes a type error in the editor.
return hands back the result; console.log only displays a message. If a function declares : number and forgets to return on some path, TypeScript warns you.
sobrenome?: string marks an optional parameter; inside the function its type is string | undefined.
Try it
resultado receives dobro(6) and is inferred as number. Call mensagem with and without a surname.
Execute o código para ver a saída aqui.Exercise
Implement calcularTotal(preco: number, quantidade: number, frete: number): number as price × quantity + shipping, with shipping added once. Zero quantity still returns the shipping. Keep the signature. Feedback is in Portuguese.
Execute o código para ver a saída aqui.Edite o TypeScript e clique em Verificar.
Next step
Next, store many inputs in a typed list.