To use the times
function from the Underscore library in TypeScript, you need to first install Underscore and its corresponding type definitions for TypeScript:
index.ts56 chars3 lines
Then, in your TypeScript file, you can import the times
function from the Underscore library and use it as follows:
index.ts163 chars6 lines
The times
function takes two arguments: the first argument is the number of times to invoke the function (n
), and the second argument is the function to invoke. The function is invoked with the index of the iteration as the argument (i
), starting at 0.
In the example above, we use the times
function to create an array of numbers from 1 to 5. We pass a function that takes the current index (i
) and adds 1 to it to create the value of the array element.
Note that the @types/underscore
package provides type definitions for the Underscore library, which allows TypeScript to check for type errors and provides auto-completion support in editors.
gistlibby LogSnag