Dev Helpers
← Back

Map through a typed callback

Map through a typed callback using TypeScript.

Open this example in the interactive library

Source code

const words = ["build", "ship", "test"];
console.log(
  (<T, U>(items: T[], fn: (item: T) => U): U[] => items.map(fn))(
    words,
    (w) => w.length,
  ),
);

Sample output

[ 5, 4, 4 ]