@@ -76,32 +76,41 @@ var t1a: string = t1.a;
76
76
var t1c = t1.c;
77
77
78
78
// Use of new to create record factories (supported, but discouraged)
79
- const PointNew = new Record({ x :0 , y :0 } );
79
+ type TPointNew = { x : number , y : number } ;
80
+ type PointNew = RecordOf< TPointNew > ;
81
+ const MakePointNew: RecordFactory< TPointNew > = new Record({ x :0 , y :0 } );
80
82
// Not using new allows returning a record.
81
- const origin: RecordOf< { x :number , y :number } > = PointNew();
82
- // Can use the Record constructor type as an alternative,
83
- // it just doesn't support property access.
84
- const originAlt1: PointNew = PointNew();
85
- // Can also sort of use the inner Record values type as an alternative,
86
- // however it does not have the immutable record API, though useful for flowing
87
- // immutable Records where plain objects are expected.
88
- const originAlt2: { x : number , y : number } = PointNew();
83
+ const origin: PointNew = MakePointNew();
89
84
// Both get and prop access are supported with RecordOf
90
85
{ const x : number = origin . get ( 'x' ) }
91
86
{ const x : number = origin . x }
92
87
// $ExpectError number is not a string
93
88
{ const x : string = origin . x }
89
+ // Can use the Record constructor type as an alternative,
90
+ // it just doesn't support property access.
91
+ const originAlt1: MakePointNew = MakePointNew();
92
+ // Both get and prop access are supported with RecordOf
93
+ { const x : number = originAlt1 . get ( 'x' ) }
94
+ // $ExpectError cannot use property access for this alternative annotation
95
+ { const x : number = originAlt1 . x }
96
+ // Can also sort of use the inner Record values type as an alternative,
97
+ // however it does not have the immutable record API, though useful for flowing
98
+ // immutable Records where plain objects are expected.
99
+ const originAlt2: TPointNew = MakePointNew();
100
+ // $ExpectError cannot use Record API for this alternative annotation
101
+ { const x : number = originAlt2 . get ( 'x' ) }
102
+ { const x : number = originAlt2 . x }
94
103
95
104
// $ExpectError Use of new may only return a class instance, not a record
96
- const mistakeOriginNew: RecordOf < { x : number , y : number } > = new PointNew ();
105
+ const mistakeOriginNew: PointNew = new MakePointNew ();
97
106
// An alternative type strategy is instance based
98
- const originNew: PointNew = new PointNew ();
107
+ const originNew: MakePointNew = new MakePointNew ();
99
108
// Only get, but not prop access are supported with class instances
100
109
{ const x : number = originNew . get ( 'x' ) }
101
110
// $ExpectError property `x`. Property not found in RecordInstance
102
111
{ const x : number = originNew . x }
103
112
104
113
// $ExpectError instantiated with invalid type
105
- const mistakeNewRecord = PointNew ({ x : 'string' } );
114
+ const mistakeNewRecord = MakePointNew ({ x : 'string' } );
106
115
// $ExpectError instantiated with invalid type
107
- const mistakeNewInstance = new PointNew ({ x : 'string' } );
116
+ const mistakeNewInstance = new MakePointNew ({ x : 'string' } );
0 commit comments