@@ -17,7 +17,7 @@ npm install --save-dev simplytyped
17
17
18
18
** [ Objects] ( #objects ) **
19
19
20
- [ AllKeys] ( #allkeys ) - [ AllRequired] ( #allrequired ) - [ CombineObjects] ( #combineobjects ) - [ DeepPartial] ( #deeppartial ) - [ DeepReadonly] ( #deepreadonly ) - [ DiffKeys] ( #diffkeys ) - [ GetKey] ( #getkey ) - [ HasKey] ( #haskey ) - [ Intersect] ( #intersect ) - [ KeysByType] ( #keysbytype ) - [ Merge] ( #merge ) - [ ObjectKeys] ( #objectkeys ) - [ ObjectType] ( #objecttype ) - [ Omit] ( #omit ) - [ Optional] ( #optional ) - [ Overwrite] ( #overwrite ) - [ PlainObject] ( #plainobject ) - [ PureKeys] ( #purekeys ) - [ Required] ( #required ) - [ SharedKeys] ( #sharedkeys ) - [ StrictUnion] ( #strictunion ) - [ StringKeys] ( #stringkeys ) - [ TaggedObject] ( #taggedobject ) - [ UnionizeProperties] ( #unionizeproperties ) - [ UnionKeys] ( #unionkeys )
20
+ [ AllKeys] ( #allkeys ) - [ AllRequired] ( #allrequired ) - [ CombineObjects] ( #combineobjects ) - [ DeepPartial] ( #deeppartial ) - [ DeepReadonly] ( #deepreadonly ) - [ DiffKeys] ( #diffkeys ) - [ ElementwiseIntersect ] ( #elementwiseintersect ) - [ GetKey] ( #getkey ) - [ HasKey] ( #haskey ) - [ Intersect] ( #intersect ) - [ KeysByType] ( #keysbytype ) - [ Merge] ( #merge ) - [ ObjectKeys] ( #objectkeys ) - [ ObjectType] ( #objecttype ) - [ Omit] ( #omit ) - [ Optional] ( #optional ) - [ Overwrite] ( #overwrite ) - [ PlainObject] ( #plainobject ) - [ PureKeys] ( #purekeys ) - [ Required] ( #required ) - [ SharedKeys] ( #sharedkeys ) - [ StrictUnion] ( #strictunion ) - [ StringKeys] ( #stringkeys ) - [ TaggedObject] ( #taggedobject ) - [ TryKey ] ( #trykey ) - [ UnionizeProperties] ( #unionizeproperties ) - [ UnionKeys] ( #unionkeys )
21
21
22
22
** [ Utils] ( #utils ) **
23
23
@@ -88,12 +88,12 @@ Useful for making extremely complex types look nice in VSCode.
88
88
``` ts
89
89
test (' Can combine two objects (without pesky & in vscode)' , t => {
90
90
type a = { x: number , y: ' hi' };
91
- type b = { z: number , y : ' there ' };
91
+ type b = { z: number };
92
92
93
93
type got = CombineObjects <a , b >;
94
94
type expected = {
95
95
x: number ,
96
- y: ' hi' & ' there ' ,
96
+ y: ' hi' ,
97
97
z: number
98
98
};
99
99
@@ -231,6 +231,51 @@ test('Can get all keys that are different between objects', t => {
231
231
});
232
232
```
233
233
234
+ ### ElementwiseIntersect
235
+ Takes two objects and returns their element-wise intersection.
236
+ * Note* : this removes any key-level information, such as optional or readonly keys.
237
+ ``` ts
238
+ test (' Can combine two objects elementwise' , t => {
239
+ type a = { x: number , y: ' hi' };
240
+ type b = { z: number , y: ' there' };
241
+
242
+ type got = ElementwiseIntersect <a , b >;
243
+ type expected = {
244
+ x: number ,
245
+ y: ' hi' & ' there' ,
246
+ z: number ,
247
+ };
248
+
249
+ assert <got , expected >(t );
250
+ assert <expected , got >(t );
251
+ });
252
+
253
+ test (' Can combine two objects with private members elementwise' , t => {
254
+ class A {
255
+ a: number = 1 ;
256
+ private x: number = 2 ;
257
+ y: ' hi' = ' hi' ;
258
+ private z: ' hey' = ' hey' ;
259
+ }
260
+
261
+ class B {
262
+ a: 22 = 22 ;
263
+ private x: number = 2 ;
264
+ y: ' there' = ' there' ;
265
+ private z: ' friend' = ' friend' ;
266
+ }
267
+
268
+ type got = ElementwiseIntersect <A , B >;
269
+ type expected = {
270
+ a: 22 ,
271
+ y: ' hi' & ' there' ,
272
+ };
273
+
274
+ assert <got , expected >(t );
275
+ assert <expected , got >(t );
276
+ });
277
+ ```
278
+
234
279
### GetKey
235
280
Gets the value of specified property on any object without compile time error (` Property 'b' does not exist on type '{ a: string; }'. ` ) and the like.
236
281
Returns ` never ` if the key is not on the object.
@@ -471,6 +516,10 @@ For discriminated unions of objects, it is important to have a single "tag" prop
471
516
Creates an object with each entry being tagged by the key defining that entry.
472
517
473
518
519
+ ### TryKey
520
+ Like ` GetKey ` , but returns ` unknown ` if the key is not present on the object.
521
+
522
+
474
523
### UnionizeProperties
475
524
Get a union of the properties of an object.
476
525
``` ts
0 commit comments