diff --git a/__tests__/Range.ts b/__tests__/Range.ts index 0a5618ff46..915dd592a8 100644 --- a/__tests__/Range.ts +++ b/__tests__/Range.ts @@ -24,8 +24,19 @@ describe('Range', () => { expect(v.toArray()).toEqual([1, 4, 7]); }); + it('range should contain start and end values', () => { + // @ts-expect-error -- test that runtime error is thrown + expect(() => Range()).toThrow( + 'You must define a start value when using Range' + ); + // @ts-expect-error -- test that runtime error is thrown + expect(() => Range(1)).toThrow( + 'You must define an end value when using Range' + ); + }); + it('open range', () => { - const v = Range(10); + const v = Range(10, Infinity); expect(v.size).toBe(Infinity); expect(v.first()).toBe(10); expect(v.rest().first()).toBe(11); diff --git a/__tests__/count.ts b/__tests__/count.ts index 8cbfb771c3..c4b13fb957 100644 --- a/__tests__/count.ts +++ b/__tests__/count.ts @@ -64,13 +64,13 @@ describe('count', () => { }); it('with infinitely long sequences of known length', () => { - const seq = Range(); + const seq = Range(0, Infinity); expect(seq.size).toBe(Infinity); expect(seq.isEmpty()).toBe(false); }); it('with infinitely long sequences of unknown length', () => { - const seq = Range().filter(x => x % 2 === 0); + const seq = Range(0, Infinity).filter(x => x % 2 === 0); expect(seq.size).toBe(undefined); expect(seq.isEmpty()).toBe(false); expect(seq.size).toBe(undefined); diff --git a/__tests__/zip.ts b/__tests__/zip.ts index 9fe2d16889..4f8e7f538a 100644 --- a/__tests__/zip.ts +++ b/__tests__/zip.ts @@ -36,7 +36,7 @@ describe('zip', () => { it('zips with infinite lists', () => { expect( - Range() + Range(0, Infinity) .zip(Seq(['A', 'B', 'C'])) .toArray() ).toEqual([ @@ -135,7 +135,7 @@ describe('zip', () => { }); it('with infinite lists', () => { - const r: Seq.Indexed = Range(); + const r: Seq.Indexed = Range(0, Infinity); const i = r.interleave(Seq(['A', 'B', 'C'])); expect(i.size).toBe(6); expect(i.toArray()).toEqual([0, 'A', 1, 'B', 2, 'C']); diff --git a/src/Range.js b/src/Range.js index 332994aef9..45945be23a 100644 --- a/src/Range.js +++ b/src/Range.js @@ -11,16 +11,21 @@ import deepEqual from './utils/deepEqual'; * infinity. When start is equal to end, returns empty list. */ export class Range extends IndexedSeq { - constructor(start, end, step) { + constructor(start, end, step = 1) { if (!(this instanceof Range)) { return new Range(start, end, step); } invariant(step !== 0, 'Cannot step a Range by 0'); - start = start || 0; - if (end === undefined) { - end = Infinity; - } - step = step === undefined ? 1 : Math.abs(step); + invariant( + start !== undefined, + 'You must define a start value when using Range' + ); + invariant( + end !== undefined, + 'You must define an end value when using Range' + ); + + step = Math.abs(step); if (end < start) { step = -step; } diff --git a/type-definitions/immutable.d.ts b/type-definitions/immutable.d.ts index a44869187e..e58b95e098 100644 --- a/type-definitions/immutable.d.ts +++ b/type-definitions/immutable.d.ts @@ -2475,8 +2475,8 @@ declare namespace Immutable { * ``` */ function Range( - start?: number, - end?: number, + start: number, + end: number, step?: number ): Seq.Indexed; diff --git a/type-definitions/ts-tests/range.ts b/type-definitions/ts-tests/range.ts index 9e8432cc67..7429c44e71 100644 --- a/type-definitions/ts-tests/range.ts +++ b/type-definitions/ts-tests/range.ts @@ -4,8 +4,14 @@ import { Range } from 'immutable'; // #constructor // $ExpectType Indexed - Range(0, 0, 0); + Range(0, 0, 1); // $ExpectError Range('a', 0, 0); + + // $ExpectError + Range(); + + // $ExpectError + Range(1); }