10
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TypeScript で range() 関数の実装

Last updated at Posted at 2018-01-31

1から10までの配列を作りたいみたいなときに使える関数の紹介です。

range.ts
export const range =
  (start: number, end: number) => Array.from({length: (end - start + 1)}, (v, k) => k + start);
使い方.ts
range(3, 6) // [3, 4, 5, 6]

range(0, 9).map((_) => {return "hoge"}) // 10個の "hoge" が入った配列を返す

すごく便利。
型情報を取ったらJavaScriptでも動きます。
ご参考まで。

10
5
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?