Skip to content

Commit 1645610

Browse files
committed
chore(docs): add param annotation tag in object-types
1 parent 84d7c65 commit 1645610

File tree

2 files changed

+78
-18
lines changed

2 files changed

+78
-18
lines changed

src/object-types.ts

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import type { ReturnTypeOf } from "./array-types.js"
88
* for better readability.
99
*
1010
* It doesn't change the original object type.
11+
*
12+
* @param {object} Obj - The object to prettify
1113
*/
1214
export type Prettify<Obj extends object> = {
1315
[Property in keyof Obj]: Obj[Property]
@@ -16,6 +18,7 @@ export type Prettify<Obj extends object> = {
1618
/**
1719
* It creates a new type based on your object but marks every property as readonly
1820
*
21+
* @param {object} Obj - The object to make readonly
1922
* @example
2023
* interface User {
2124
* name: string,
@@ -37,28 +40,35 @@ export type DeepReadonly<Obj extends object> = {
3740
}
3841

3942
/**
40-
* Compare two types and return `never` if the type `T` extends the type `Match`,
41-
* otherwise return the type `T`.
42-
*
43+
* Conditionally excludes or includes types based on whether `Type` is assignable to `Extends`.
44+
* If `Reverse` is `true`, it includes types that are assignable to `Extends`, otherwise it excludes them.
45+
* If `Value` is provided, it returns `Value` instead of `never` when the condition is met.
46+
*
47+
* @param {Type} Type - The type to evaluate
48+
* @param {Extends} Extends - The type to compare against
49+
* @param {Value} Value - The value to return if the condition is met (defaults to `never`)
50+
* @param {Reverse} Reverse - If `true`, reverses the condition (defaults to `false`)
4351
* @example
4452
* // Expected: number
4553
* type A = Discard<string | number, string>;
54+
*
4655
* // Expected: string | number
4756
* type B = Discard<string | number, boolean>;
4857
*/
49-
export type Discard<Compare, Match, Value = never, Reverse extends boolean = false> = Reverse extends true
50-
? Compare extends Match
58+
export type Discard<Type, Extends, Value = never, Reverse extends boolean = false> = Reverse extends true
59+
? Type extends Extends
5160
? Value
5261
: never
53-
: Compare extends Match
62+
: Type extends Extends
5463
? never
5564
: IsNever<Value> extends true
56-
? Compare
65+
? Type
5766
: Value
5867

5968
/**
6069
* Get the type of the resolved value of a PromiseLike object.
6170
*
71+
* @param {PromiseLike<unknown>} T - The PromiseLike object to resolve
6272
* @example
6373
* // Expected: string
6474
* type A = Awaited<Promise<string>>;
@@ -76,6 +86,8 @@ export type Awaited<T extends PromiseLike<unknown>> =
7686
/**
7787
* Creates a union of the keys of two objects
7888
*
89+
* @param {object} Obj1 - The first object to get the keys from
90+
* @param {object} Obj2 - The second object to get the keys from
7991
* @example
8092
* interface Foo {
8193
* foo: string,
@@ -96,6 +108,8 @@ export type Properties<Obj1 extends object, Obj2 extends object, Extends extends
96108
* Creates a new object by merging two objects. Properties from `Obj1` override properties
97109
* from `Obj2` if they have the same key
98110
*
111+
* @param {object} Obj1 - The first object to merge
112+
* @param {object} Obj2 - The second object to merge
99113
* @example
100114
* interface Config {
101115
* storePaths: string[],
@@ -124,6 +138,8 @@ type IntersectionImplementation<Obj1 extends object, Obj2 extends object, Keys =
124138
/**
125139
* Create a new object based in the difference keys between the objects.
126140
*
141+
* @param {object} Obj1 - The first object to compare
142+
* @param {object} Obj2 - The second object to compare
127143
* @example
128144
* interface Foo {
129145
* name: string
@@ -142,8 +158,10 @@ type IntersectionImplementation<Obj1 extends object, Obj2 extends object, Keys =
142158
export type Intersection<Obj1 extends object, Obj2 extends object> = IntersectionImplementation<Obj1, Obj2>
143159

144160
/**
145-
* Create a new object based in the type of its keys
161+
* Create a new object based in the keys that are assignable of type `Type`
146162
*
163+
* @param {object} Obj - The object to pick the keys from
164+
* @param {Type} Type - The type to compare against
147165
* @example
148166
* interface User {
149167
* name: string,
@@ -159,8 +177,11 @@ export type PickByType<Obj extends object, Type> = {
159177
}
160178

161179
/**
162-
* Converts the specified keys of an object into optional ones
180+
* Converts the specified `Keys` of an object type into optional ones, By default it makes
181+
* all properties optional.
163182
*
183+
* @param {object} Obj - The object to convert
184+
* @param {string} Keys - The keys to make optional
164185
* @example
165186
* interface User {
166187
* name: string,
@@ -178,6 +199,8 @@ export type PartialByKeys<Obj extends object, Keys extends keyof Obj = keyof Obj
178199
/**
179200
* Create a new object based in the keys that are not assignable of type `Type`
180201
*
202+
* @param {object} Obj - The object to pick the keys from
203+
* @param {Type} Type - The type to compare against
181204
* @example
182205
* interface User {
183206
* name: string,
@@ -196,6 +219,8 @@ export type OmitByType<Obj extends object, Type> = {
196219
* Extracts the value of a key from an object and returns a new object with that value,
197220
* while keeping the other values unchanged.
198221
*
222+
* @param {object} Obj - The object to extract the value from
223+
* @param {string} Key - The key to extract the value from
199224
* @example
200225
* interface User {
201226
* name: string,
@@ -220,6 +245,7 @@ export type FlattenProperties<Obj extends object, Keys extends keyof Obj> = Pret
220245
/**
221246
* Removes the properties whose keys start with an underscore (_).
222247
*
248+
* @param {object} Obj - The object to remove the properties from
223249
* @example
224250
* interface User {
225251
* name: string,
@@ -238,8 +264,10 @@ export type PublicOnly<Obj extends object> = {
238264
* Checks if a key exists in either of the two objects and returns its value.
239265
* If the key does not exist in either object, it returns `never`.
240266
*
267+
* @param {object} Obj1 - The first object to check
268+
* @param {object} Obj2 - The second object to check
269+
* @param {string} Key - The key to check
241270
* @example
242-
*
243271
* interface Foo {
244272
* foo: string
245273
* }
@@ -262,8 +290,11 @@ export type RetrieveKeyValue<Obj1 extends object, Obj2 extends object, Key> = Ke
262290

263291
/**
264292
* Convert to required the keys speficied in the type `Keys`, and the others fields mantein
265-
* their definition. When `Keys` is not provided, it should make all properties required
293+
* their definition. When `Keys` is not provided, it should make all properties required. By default
294+
* it makes all properties required.
266295
*
296+
* @param {object} Obj - The object to convert
297+
* @param {string} Keys - The keys to make required
267298
* @example
268299
* interface User {
269300
* name?: string,
@@ -282,6 +313,8 @@ export type RequiredByKeys<Obj extends object, Keys extends keyof Obj = keyof Ob
282313
*
283314
* Merge the properties of two objects and it the properties are repeated the types create an union
284315
*
316+
* @param {object} Obj1 - The first object to merge
317+
* @param {object} Obj2 - The second object to merge
285318
* @example
286319
* interface Foo {
287320
* bar: string
@@ -309,6 +342,7 @@ export type UnionMerge<Obj1 extends object, Obj2 extends object> = {
309342
*
310343
* **Important:** Only affects top-level properties, not nested properties.
311344
*
345+
* @param {object} Obj - The object to convert
312346
* @example
313347
* interface User {
314348
* readonly name: string;
@@ -327,6 +361,7 @@ export type Mutable<Obj extends object> = {
327361
* Converts all properties to non-readonly of alls levels of the object type,
328362
* This is an advanced utility type of `Mutable`
329363
*
364+
* @param {object} Obj - The object to convert
330365
* @example
331366
* interface Foo {
332367
* readonly foo: {
@@ -357,6 +392,7 @@ type MergeAllImplementation<Array extends readonly object[], Merge extends objec
357392
* Create a new object type based in the tuple of object types, if the properties
358393
* are duplicated will create an union type.
359394
*
395+
* @param {T[]} Array - The tuple of object types to merge
360396
* @example
361397
* interface Foo {
362398
* foo: string
@@ -380,6 +416,9 @@ export type MergeAll<Array extends readonly object[]> = MergeAllImplementation<A
380416
/**
381417
* Create a new object type appending a new property with its value
382418
*
419+
* @param {object} Obj - The object to append the property
420+
* @param {string} NewProp - The new property to append
421+
* @param {TypeValue} TypeValue - The type of the new property
383422
* @example
384423
* interface User {
385424
* name: string
@@ -395,6 +434,7 @@ export type AddPropertyToObject<Obj extends object, NewProp extends string, Type
395434
/**
396435
* Returns a union type of the entries of the provided object
397436
*
437+
* @param {object} Obj - The object to get the entries from
398438
* @example
399439
* interface Foo {
400440
* foo: string,
@@ -413,6 +453,10 @@ export type ObjectEntries<Obj extends object, RequiredObj extends object = Requi
413453
* Replaces the types of the keys in an object with new types defined in the `Replace` object.
414454
* If a key in `Keys` is not found in `Replace`, it defaults to the `Default` type.
415455
*
456+
* @param {object} Obj - The object to replace the keys
457+
* @param {string} Keys - The keys to replace
458+
* @param {object} Replace - The new types to replace the keys
459+
* @param {unknown} Default - The default type if the key is not found in `Replace`
416460
* @example
417461
* interface Foo {
418462
* foo: string,
@@ -435,6 +479,8 @@ export type ReplaceKeys<Obj extends object, Keys extends string, Replace extends
435479
* Transforms the types of the keys in an object that match the `from` type in the `Mapper`,
436480
* replacing them with the `to` type in the `Mapper`.
437481
*
482+
* @param {object} Obj - The object to map the types
483+
* @param {object} Mapper - The types to map
438484
* @example
439485
* // Expected: { foo: string, bar: boolean }
440486
* type ReplaceTypesI = MapTypes<{ foo: string, bar: number }, { from: number, to: boolean }>;
@@ -453,8 +499,11 @@ export type MapTypes<Obj extends object, Mapper extends { from: unknown; to: unk
453499
}
454500

455501
/**
456-
* Omits properties of an object at any depth based on the provided pattern.
502+
* Omits properties of an object at any depth based on the provided pattern string that
503+
* is a dot-separated path to the property.
457504
*
505+
* @param {object} Obj - The object to omit the properties from
506+
* @param {string} Pattern - The pattern to omit the properties
458507
* @example
459508
* type User = {
460509
* name: string,
@@ -488,6 +537,7 @@ export type DeepOmit<Obj extends object, Pattern extends string> = {
488537
* Transforms the object properties to their primitive types. If the properties are objects,
489538
* it recursively transforms their properties to their primitive types, and so on.
490539
*
540+
* @param {object} Obj - The object to transform
491541
* @example
492542
* interface User {
493543
* name: "Foo",
@@ -522,6 +572,7 @@ type GetRequiredImplementation<Obj extends object, RequiredKeys extends object =
522572
* Get only the keys of an object that are required in the object type otherwise
523573
* remove them from the object type.
524574
*
575+
* @param {object} Obj - The object to get the required keys from
525576
* @example
526577
* interface User {
527578
* name: string
@@ -538,6 +589,7 @@ export type GetRequired<Obj extends object> = GetRequiredImplementation<Obj>
538589
* Get only the keys of an object that are optional in the object type otherwise
539590
* remove them from the object type
540591
*
592+
* @param {object} Obj - The object to get the optional keys from
541593
* @example
542594
* interface User {
543595
* name: string
@@ -556,6 +608,8 @@ export type GetOptional<T extends object> = {
556608
* Get the value of a key from an object without worrying about the nested properties
557609
* of the object only should separate the keys with a dot.
558610
*
611+
* @param {object} T - The object to get the value from
612+
* @param {string} K - The key to get the value from
559613
* @example
560614
* interface User {
561615
* foo: {
@@ -580,8 +634,13 @@ export type Get<T, K extends string> = K extends keyof T
580634
: never
581635

582636
/**
637+
* TODO: This type is the same as `Get` type, but it should be removed in the future or just
638+
* update the logic to make it different from `Get` type.
639+
*
583640
* Picks the properties of an object at any depth based on the provided pattern.
584641
*
642+
* @param {object} Obj - The object to pick the properties from
643+
* @param {string} Pattern - The pattern to pick the properties
585644
* @example
586645
* interface User {
587646
* name: string,
@@ -591,8 +650,9 @@ export type Get<T, K extends string> = K extends keyof T
591650
* }
592651
* }
593652
*
594-
* // Expected: { address: { street: string } }
653+
* // Expected: string
595654
* type UserPick = DeepPick<User, "address.street">
655+
*
596656
*/
597657
export type DeepPick<Obj, Pattern> = Pattern extends `${infer Left}.${infer Right}`
598658
? Left extends keyof Obj

test/object-types.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,9 @@ describe("DeepPick", () => {
405405
}
406406
}
407407

408-
expectTypeOf<utilities.DeepPick<Obj, "foo">>().toEqualTypeOf<string>()
409-
expectTypeOf<utilities.DeepPick<Obj, "bar">>().toEqualTypeOf<number>()
410-
expectTypeOf<utilities.DeepPick<Obj, "foobar">>().toEqualTypeOf<{
408+
expectTypeOf<utilities.Get<Obj, "foo">>().toEqualTypeOf<string>()
409+
expectTypeOf<utilities.Get<Obj, "bar">>().toEqualTypeOf<number>()
410+
expectTypeOf<utilities.Get<Obj, "foobar">>().toEqualTypeOf<{
411411
foofoo: number
412412
barbar: boolean
413413
foo: {
@@ -419,8 +419,8 @@ describe("DeepPick", () => {
419419
}
420420
}
421421
}>()
422-
expectTypeOf<utilities.DeepPick<Obj, "foobar.barbar">>().toEqualTypeOf<boolean>()
423-
expectTypeOf<utilities.DeepPick<Obj, "foobar.foo.barfoo">>().toEqualTypeOf<{
422+
expectTypeOf<utilities.Get<Obj, "foobar.barbar">>().toEqualTypeOf<boolean>()
423+
expectTypeOf<utilities.Get<Obj, "foobar.foo.barfoo">>().toEqualTypeOf<{
424424
foobar: string
425425
bar: number
426426
}>()

0 commit comments

Comments
 (0)