type-fest 4.26.0 → 4.26.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/readme.md +47 -0
- package/source/exact.d.ts +8 -7
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -392,6 +392,53 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
|
|
|
392
392
|
|
|
393
393
|
There are many advanced types most users don't know about.
|
|
394
394
|
|
|
395
|
+
|
|
396
|
+
- [`Awaited<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#awaitedtype) - Extract the type of a value that a `Promise` resolves to.
|
|
397
|
+
<details>
|
|
398
|
+
<summary>
|
|
399
|
+
Example
|
|
400
|
+
</summary>
|
|
401
|
+
|
|
402
|
+
[Playground](https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgKoGdrIN4FgBQyyAkMACYBcyIArgLYBG0A3AUcSHHRFemFKADmrQiTiCe1ekygiiAXwJtkCADZx06NJigBBAA7AAytABuwJDmXENATxAJkMCGAQALDNAAUNHQElKKUZoAEoqAAUoAHs6YEwAHk8oAD4rUWJiAHpM5AAxF3dkMDcUXywyODA4J2i6IpLkCqqGDQgAOmssnIAVBsQwGjhVZGA6fVUIbnBK4CiQZFjBNzBkVSiogGtV4A2UYriKTuyVOb5kKAh0fVOUAF5kOAB3OGAV51c3LwAiTLhDTLKUEyABJsICAvIQnISF0TiAzk1qvcLlcbm0AFboOZeKFHHIXAZQeaI6EZAk0Ik4EaBACMABpqFxJF8AFJRNzzAAiUQgXwZ4kkAGYAAzIeSkxSiSXKMC2fQofIfCBkJLIe66Z6vZXxABKLgpIG6cogiR0BmMZgsEAA2l93u4kl8ALrJZIiZR2BxOGgOMCzeZuOAgMgTJKcypwLx-C1QcxIKhJc0mWNWhngwK0YJQEJpdj8Wy5mEIU4rQFURXuZWq+5PF4raPJuPte0eHQ+fxkXHpWG6GCQKBOApuITIQGNCMM2xRGgqIPIeWwKJQOqmOACadafr+rToGiFDSj-RNEfFUo6EbgaDwJB0vGz9wnhqImpRb2Es8QBlLhZwDYjuBkGQrz+kMyC6OEfjnBAACONCXGAm5aCAEDKsqHTpPIs4fMgXjQNE2aFhkxx4d+gbBqoQjWJKChKKIxbwqWZqGI2VpqtQECPNo0BJpaSA4tCZEhhAYYRu23HMbxn7IDSUJAA)
|
|
403
|
+
|
|
404
|
+
```ts
|
|
405
|
+
interface User {
|
|
406
|
+
id: number;
|
|
407
|
+
name: string;
|
|
408
|
+
age: number;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
class UserApiService {
|
|
412
|
+
async fetchUser(userId: number): Promise<User> {
|
|
413
|
+
// Fetch the user data from the database.
|
|
414
|
+
// The actual implementation might look like this:
|
|
415
|
+
// const response = await fetch('/api/user/${userId}');
|
|
416
|
+
// const data = response.json();
|
|
417
|
+
// return data;
|
|
418
|
+
return {
|
|
419
|
+
id: 1,
|
|
420
|
+
name: 'John Doe',
|
|
421
|
+
age: 30
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
type FetchedUser = Awaited<ReturnType<UserApiService['fetchUser']>>;
|
|
427
|
+
|
|
428
|
+
async function handleUserData(apiService: UserApiService, userId: number) {
|
|
429
|
+
try {
|
|
430
|
+
const user: FetchedUser = await apiService.fetchUser(userId);
|
|
431
|
+
// After fetching user data, you can perform various actions such as updating the user interface,
|
|
432
|
+
// caching the data for future use, or making additional API requests as needed.
|
|
433
|
+
} catch (error) {
|
|
434
|
+
// Error handling
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const userApiService = new UserApiService();
|
|
439
|
+
handleUserData(userApiService, 1);
|
|
440
|
+
```
|
|
441
|
+
|
|
395
442
|
- [`Partial<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype) - Make all properties in `T` optional.
|
|
396
443
|
<details>
|
|
397
444
|
<summary>
|
package/source/exact.d.ts
CHANGED
|
@@ -53,13 +53,14 @@ onlyAcceptNameImproved(invalidInput); // Compilation error
|
|
|
53
53
|
@category Utilities
|
|
54
54
|
*/
|
|
55
55
|
export type Exact<ParameterType, InputType> =
|
|
56
|
-
//
|
|
57
|
-
ParameterType extends
|
|
58
|
-
// If the parameter is
|
|
59
|
-
:
|
|
60
|
-
// If the parameter is
|
|
61
|
-
: ParameterType extends
|
|
62
|
-
|
|
56
|
+
// Before distributing, check if the two types are equal and if so, return the parameter type immediately
|
|
57
|
+
IsEqual<ParameterType, InputType> extends true ? ParameterType
|
|
58
|
+
// If the parameter is a primitive, return it as is immediately to avoid it being converted to a complex type
|
|
59
|
+
: ParameterType extends Primitive ? ParameterType
|
|
60
|
+
// If the parameter is an unknown, return it as is immediately to avoid it being converted to a complex type
|
|
61
|
+
: IsUnknown<ParameterType> extends true ? unknown
|
|
62
|
+
// If the parameter is a Function, return it as is because this type is not capable of handling function, leave it to TypeScript
|
|
63
|
+
: ParameterType extends Function ? ParameterType
|
|
63
64
|
// Convert union of array to array of union: A[] & B[] => (A & B)[]
|
|
64
65
|
: ParameterType extends unknown[] ? Array<Exact<ArrayElement<ParameterType>, ArrayElement<InputType>>>
|
|
65
66
|
// In TypeScript, Array is a subtype of ReadonlyArray, so always test Array before ReadonlyArray.
|