I have to write a function that takes an array of tuples, each tuple consists of a name and an age. The function should return only the names.
So I wrote it like this:
type someTuple = [string, number]
function names(namesAndAges: someTuple[]) {
let allNames: string[]
allNames.push(namesAndAges.forEach( nameAndAge => nameAndAge[0]))
return allNames
}
When I call it with this:
names([['Amir', 34], ['Betty', 17]]);
I get this error:
type error: type error: Variable 'allNames' is used before being assigned.
Can anyone point what is wrong with this code?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…