← Trail· Lesson 7 of 10
See this lesson in JavaScript →
Arrays: loop, sum and find the largest
Use typed arrays, loop with for...of and handle empty lists with number | null.

An array stores values in order. number[] means a list of numbers; numeros[0] is the first item and numeros.length the size. Inside for (const numero of numeros), TypeScript knows numero is a number.
readonly number[] promises the function only reads the list: numeros.push(1) inside it is a type error. An empty list has no largest number, so the return type number | null makes callers handle null. Zero is a valid result, not absence.
Try it
Make the values negative and check the sum and the largest. In an empty list, numeros[0] is undefined.
Execute o código para ver a saída aqui.Exercise
maiorNumero(numeros: readonly number[]): number | null already handles the empty list. Fix how maior starts so negative numbers work. Do not change the input. Feedback is in Portuguese.
Execute o código para ver a saída aqui.Edite o TypeScript e clique em Verificar.
Next step
Next, objects and interfaces group values by meaning.