Skip to content

Commit e65e5af

Browse files
committed
Improve docs around Record Types and classes
Fixes #1495
1 parent 79280d4 commit e65e5af

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

__tests__/Record.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ describe('Record', () => {
4848
it('setting an unknown key is a no-op', () => {
4949
const MyType = Record({ a: 1, b: 2, c: 3 });
5050

51-
const t1 = new MyType({ a: 10, b: 20 });
51+
const t1 = MyType({ a: 10, b: 20 });
5252
const t2 = t1.set('d' as any, 4);
5353

5454
expect(t2).toBe(t1);
5555
});
5656

5757
it('falls back to default values when deleted or cleared', () => {
5858
const MyType = Record({ a: 1, b: 2, c: 3 });
59-
const t1 = new MyType({ a: 10, b: 20 });
60-
const t2 = new MyType({ b: 20 });
59+
const t1 = MyType({ a: 10, b: 20 });
60+
const t2 = MyType({ b: 20 });
6161
const t3 = t1.delete('a');
6262
const t4 = t3.clear();
6363

@@ -68,13 +68,13 @@ describe('Record', () => {
6868

6969
expect(t2.equals(t3)).toBe(true);
7070
expect(t2.equals(t4)).toBe(false);
71-
expect(t4.equals(new MyType())).toBe(true);
71+
expect(t4.equals(MyType())).toBe(true);
7272
});
7373

7474
it('allows deletion of values deep within a tree', () => {
7575
const AType = Record({ a: 1 });
76-
const BType = Record({ b: new AType({ a: 2 }) });
77-
const t1 = new BType();
76+
const BType = Record({ b: AType({ a: 2 }) });
77+
const t1 = BType();
7878
const t2 = t1.deleteIn(['b', 'a']);
7979

8080
expect(t1.get('b').get('a')).toBe(2);
@@ -90,7 +90,7 @@ describe('Record', () => {
9090

9191
it('if compared against undefined or null should return false', () => {
9292
const MyType = Record({ a: 1, b: 2 });
93-
const t1 = new MyType();
93+
const t1 = MyType();
9494
expect(t1.equals(undefined)).toBeFalsy();
9595
expect(t1.equals(null)).toBeFalsy();
9696
});
@@ -115,7 +115,7 @@ describe('Record', () => {
115115
it('converts sequences to records', () => {
116116
const MyType = Record({ a: 1, b: 2, c: 3 });
117117
const seq = Seq({ a: 10, b: 20 });
118-
const t = new MyType(seq);
118+
const t = MyType(seq);
119119
expect(t.toObject()).toEqual({ a: 10, b: 20, c: 3 });
120120
});
121121

@@ -129,7 +129,7 @@ describe('Record', () => {
129129
it('skips unknown keys', () => {
130130
const MyType = Record({ a: 1, b: 2 });
131131
const seq = Seq({ b: 20, c: 30 });
132-
const t = new MyType(seq);
132+
const t = MyType(seq);
133133

134134
expect(t.get('a')).toEqual(1);
135135
expect(t.get('b')).toEqual(20);
@@ -138,18 +138,18 @@ describe('Record', () => {
138138

139139
it('returns itself when setting identical values', () => {
140140
const MyType = Record({ a: 1, b: 2 });
141-
const t1 = new MyType();
142-
const t2 = new MyType({ a: 1 });
141+
const t1 = MyType();
142+
const t2 = MyType({ a: 1 });
143143
const t3 = t1.set('a', 1);
144144
const t4 = t2.set('a', 1);
145145
expect(t3).toBe(t1);
146146
expect(t4).toBe(t2);
147147
});
148148

149-
it('returns new record when setting new values', () => {
149+
it('returns record when setting values', () => {
150150
const MyType = Record({ a: 1, b: 2 });
151-
const t1 = new MyType();
152-
const t2 = new MyType({ a: 1 });
151+
const t1 = MyType();
152+
const t2 = MyType({ a: 1 });
153153
const t3 = t1.set('a', 3);
154154
const t4 = t2.set('a', 3);
155155
expect(t3).not.toBe(t1);
@@ -158,7 +158,7 @@ describe('Record', () => {
158158

159159
it('allows for readonly property access', () => {
160160
const MyType = Record({ a: 1, b: 'foo' });
161-
const t1 = new MyType();
161+
const t1 = MyType();
162162
const a: number = t1.a;
163163
const b: string = t1.b;
164164
expect(a).toEqual(1);
@@ -179,6 +179,7 @@ describe('Record', () => {
179179
}
180180
}
181181

182+
// Note: `new` is only used because of `class`
182183
const t1 = new ABClass({ a: 1 });
183184
const t2 = t1.setA(3);
184185
const t3 = t2.setB(10);

__tests__/RecordJS.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
const { Record } = require('../');
99

1010
describe('Record', () => {
11-
it('defines a constructor', () => {
11+
it('defines a record factory', () => {
1212
const MyType = Record({ a: 1, b: 2, c: 3 });
1313

14-
const t = new MyType();
14+
const t = MyType();
1515
const t2 = t.set('a', 10);
1616

1717
expect(t.a).toBe(1);
@@ -44,6 +44,7 @@ describe('Record', () => {
4444
}
4545
}
4646

47+
// Note: `new` is only used because of `class`
4748
const t = new Alphabet();
4849
const t2 = t.set('b', 200);
4950

type-definitions/Immutable.d.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,17 +2229,19 @@ declare module Immutable {
22292229
* myRecord.b = 5 // throws Error
22302230
* ```
22312231
*
2232-
* Record Classes can be extended as well, allowing for custom methods on your
2232+
* Record Types can be extended as well, allowing for custom methods on your
22332233
* Record. This is not a common pattern in functional environments, but is in
22342234
* many JS programs.
22352235
*
2236-
* However Record Classes are more restricted than typical JavaScript classes.
2236+
* However Record Types are more restricted than typical JavaScript classes.
22372237
* They do not use a class constructor, which also means they cannot use
22382238
* class properties (since those are technically part of a constructor).
22392239
*
2240-
* It's useful to think of Record Classes more like a Record Factory where
2241-
* record instances returned from the factory have additional API methods. It
2242-
* is best practice to not use `new` when creating new Records.
2240+
* While Record Types can be syntactically created with the JavaScript `class`
2241+
* form, the resulting Record function is actually a factory function, not a
2242+
* class constructor. Even though Record Types are not classes, JavaScript
2243+
* currently requires the use of `new` when creating new Record instances if
2244+
* they are defined as a `class`.
22432245
*
22442246
* ```
22452247
* class ABRecord extends Record({ a: 1, b: 2 }) {
@@ -2248,7 +2250,7 @@ declare module Immutable {
22482250
* }
22492251
* }
22502252
*
2251-
* var myRecord = ABRecord({b: 3})
2253+
* var myRecord = new ABRecord({b: 3})
22522254
* myRecord.getAB() // 4
22532255
* ```
22542256
*

type-definitions/tests/record.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ const originAlt2: TPointNew = MakePointNew();
106106
{ const x: number = originAlt2.get('x') }
107107
{ const x: number = originAlt2.x }
108108

109-
// $ExpectError Use of new may only return a class instance, not a record
109+
// Use of new may only return a class instance, not a record
110+
// (supported but discouraged)
111+
// $ExpectError
110112
const mistakeOriginNew: PointNew = new MakePointNew();
111113
// An alternative type strategy is instance based
112114
const originNew: MakePointNew = new MakePointNew();

0 commit comments

Comments
 (0)