// sample input
let words: string[] = [ "apple", "banana" ]
// potential type definition
type Pair = [string, number];
// detailed function declaration
function wordLength(word: string): Pair
{
return [word, word.length];
}
// various approaches using .map()
// yielding the same outcome for syntax variation
let result0 = words.map( w => wordLength(w) );
let result1 = words.map( (w: string): Pair => wordLength(w) );
let result2 = words.map( (w: string): Pair =>
{
return [w, w.length]
});
let result3 = words.map( (w: string): [string, number] =>
{
return [w, w.length]
});
let result4 = words.map( (w): [string, number] =>
{
return [w, w.length]
});
let result5 = words.map( (w): [string, number] => [w, w.length] );
Test it live with immediate error highlighting www.typescriptlang.org