File size: 28,684 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 |
import type { MergeParameters } from './versionedTypes'
import type { weakMapMemoize } from './weakMapMemoize'
export type { MergeParameters } from './versionedTypes'
/*
* -----------------------------------------------------------------------------
* -----------------------------------------------------------------------------
*
* Reselect Data Types
*
* -----------------------------------------------------------------------------
* -----------------------------------------------------------------------------
*/
/**
* A standard selector function.
* @template State - The first value, often a Redux root state object.
* @template Result - The final result returned by the selector.
* @template Params - All additional arguments passed into the selector.
*
* @public
*/
export type Selector<
State = any,
Result = unknown,
Params extends readonly any[] = any[]
> = Distribute<
/**
* A function that takes a state and returns data that is based on that state.
*
* @param state - The first argument, often a Redux root state object.
* @param params - All additional arguments passed into the selector.
* @returns A derived value from the state.
*/
(state: State, ...params: FallbackIfNever<Params, []>) => Result
>
/**
* An array of input selectors.
*
* @public
*/
export type SelectorArray<State = any> = readonly Selector<State>[]
/**
* Extracts an array of all return types from all input selectors.
*
* @public
*/
export type SelectorResultArray<Selectors extends SelectorArray> =
ExtractReturnType<Selectors>
/**
* The options object used inside `createSelector` and `createSelectorCreator`.
*
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
* @template OverrideMemoizeFunction - The type of the optional `memoize` function that could be passed into the options object inside `createSelector` to override the original `memoize` function that was initially passed into `createSelectorCreator`.
* @template OverrideArgsMemoizeFunction - The type of the optional `argsMemoize` function that could be passed into the options object inside `createSelector` to override the original `argsMemoize` function that was initially passed into `createSelectorCreator`. If none was initially provided, `weakMapMemoize` will be used.
*
* @public
*/
export interface CreateSelectorOptions<
MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize,
ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize,
OverrideMemoizeFunction extends UnknownMemoizer = never,
OverrideArgsMemoizeFunction extends UnknownMemoizer = never
> {
/**
* Reselect performs additional checks in development mode to help identify
* and warn about potential issues in selector behavior. This option
* allows you to customize the behavior of these checks per selector.
*
* @see {@link https://reselect.js.org/api/development-only-stability-checks Development-Only Stability Checks}
*
* @since 5.0.0
*/
devModeChecks?: Partial<DevModeChecks>
/**
* The memoize function that is used to memoize the {@linkcode OutputSelectorFields.resultFunc resultFunc}
* inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
*
* When passed directly into `createSelector`, it overrides the `memoize` function initially passed into `createSelectorCreator`.
*
* @example
* ```ts
* import { createSelector, weakMapMemoize } from 'reselect'
*
* const selectItemsByCategory = createSelector(
* [
* (state: RootState) => state.items,
* (state: RootState, category: string) => category
* ],
* (items, category) => items.filter(item => item.category === category),
* { memoize: weakMapMemoize }
* )
* ```
*
* @since 5.0.0
*/
memoize?: FallbackIfNever<OverrideMemoizeFunction, MemoizeFunction>
/**
* The optional memoize function that is used to memoize the arguments
* passed into the output selector generated by `createSelector`
* (e.g., `lruMemoize` or `weakMapMemoize`).
*
* When passed directly into `createSelector`, it overrides the
* `argsMemoize` function initially passed into `createSelectorCreator`.
* If none was initially provided, `weakMapMemoize` will be used.
*
* @example
* ```ts
* import { createSelector, weakMapMemoize } from 'reselect'
*
* const selectItemsByCategory = createSelector(
* [
* (state: RootState) => state.items,
* (state: RootState, category: string) => category
* ],
* (items, category) => items.filter(item => item.category === category),
* { argsMemoize: weakMapMemoize }
* )
* ```
*
* @default weakMapMemoize
*
* @since 5.0.0
*/
argsMemoize?: FallbackIfNever<
OverrideArgsMemoizeFunction,
ArgsMemoizeFunction
>
/**
* Optional configuration options for the {@linkcode CreateSelectorOptions.memoize memoize} function.
* These options are passed to the {@linkcode CreateSelectorOptions.memoize memoize} function as the second argument.
*
* @since 5.0.0
*/
memoizeOptions?: OverrideMemoizeOptions<
MemoizeFunction,
OverrideMemoizeFunction
>
/**
* Optional configuration options for the {@linkcode CreateSelectorOptions.argsMemoize argsMemoize} function.
* These options are passed to the {@linkcode CreateSelectorOptions.argsMemoize argsMemoize} function as the second argument.
*
* @since 5.0.0
*/
argsMemoizeOptions?: OverrideMemoizeOptions<
ArgsMemoizeFunction,
OverrideArgsMemoizeFunction
>
}
/**
* The additional fields attached to the output selector generated by `createSelector`.
*
* **Note**: Although {@linkcode CreateSelectorOptions.memoize memoize}
* and {@linkcode CreateSelectorOptions.argsMemoize argsMemoize} are included in the attached fields,
* the fields themselves are independent of the type of
* {@linkcode CreateSelectorOptions.memoize memoize} and {@linkcode CreateSelectorOptions.argsMemoize argsMemoize} functions.
* Meaning this type is not going to generate additional fields based on what functions we use to memoize our selectors.
*
* _This type is not to be confused with {@linkcode ExtractMemoizerFields ExtractMemoizerFields}._
*
* @template InputSelectors - The type of the input selectors.
* @template Result - The type of the result returned by the `resultFunc`.
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
*
* @public
*/
export type OutputSelectorFields<
InputSelectors extends SelectorArray = SelectorArray,
Result = unknown,
MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize,
ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize
> = {
/**
* The final function passed to `createSelector`. Otherwise known as the `combiner`.
*/
resultFunc: Combiner<InputSelectors, Result>
/**
* The memoized version of {@linkcode OutputSelectorFields.resultFunc resultFunc}.
*/
memoizedResultFunc: Combiner<InputSelectors, Result> &
ExtractMemoizerFields<MemoizeFunction>
/**
* @Returns The last result calculated by {@linkcode OutputSelectorFields.memoizedResultFunc memoizedResultFunc}.
*/
lastResult: () => Result
/**
* The array of the input selectors used by `createSelector` to compose the
* combiner ({@linkcode OutputSelectorFields.memoizedResultFunc memoizedResultFunc}).
*/
dependencies: InputSelectors
/**
* Counts the number of times {@linkcode OutputSelectorFields.memoizedResultFunc memoizedResultFunc} has been recalculated.
*/
recomputations: () => number
/**
* Resets the count of {@linkcode OutputSelectorFields.recomputations recomputations} count to 0.
*/
resetRecomputations: () => void
/**
* Counts the number of times the input selectors ({@linkcode OutputSelectorFields.dependencies dependencies})
* have been recalculated. This is distinct from {@linkcode OutputSelectorFields.recomputations recomputations},
* which tracks the recalculations of the result function.
*
* @since 5.0.0
*/
dependencyRecomputations: () => number
/**
* Resets the count {@linkcode OutputSelectorFields.dependencyRecomputations dependencyRecomputations}
* for the input selectors ({@linkcode OutputSelectorFields.dependencies dependencies})
* of a memoized selector.
*
* @since 5.0.0
*/
resetDependencyRecomputations: () => void
} & Simplify<
Required<
Pick<
CreateSelectorOptions<MemoizeFunction, ArgsMemoizeFunction>,
'argsMemoize' | 'memoize'
>
>
>
/**
* Represents the actual selectors generated by `createSelector`.
*
* @template InputSelectors - The type of the input selectors.
* @template Result - The type of the result returned by the `resultFunc`.
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
*
* @public
*/
export type OutputSelector<
InputSelectors extends SelectorArray = SelectorArray,
Result = unknown,
MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize,
ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize
> = Selector<
GetStateFromSelectors<InputSelectors>,
Result,
GetParamsFromSelectors<InputSelectors>
> &
ExtractMemoizerFields<ArgsMemoizeFunction> &
OutputSelectorFields<
InputSelectors,
Result,
MemoizeFunction,
ArgsMemoizeFunction
>
/**
* A function that takes input selectors' return values as arguments and returns a result. Otherwise known as `resultFunc`.
*
* @template InputSelectors - An array of input selectors.
* @template Result - Result returned by `resultFunc`.
*
* @public
*/
export type Combiner<InputSelectors extends SelectorArray, Result> = Distribute<
/**
* A function that takes input selectors' return values as arguments and returns a result. Otherwise known as `resultFunc`.
*
* @param resultFuncArgs - Return values of input selectors.
* @returns The return value of {@linkcode OutputSelectorFields.resultFunc resultFunc}.
*/
(...resultFuncArgs: SelectorResultArray<InputSelectors>) => Result
>
/**
* A standard function returning true if two values are considered equal.
*
* @public
*/
export type EqualityFn<T = any> = (a: T, b: T) => boolean
/**
* The frequency of development mode checks.
*
* @since 5.0.0
* @public
*/
export type DevModeCheckFrequency = 'always' | 'once' | 'never'
/**
* Represents the configuration for development mode checks.
*
* @since 5.0.0
* @public
*/
export interface DevModeChecks {
/**
* Overrides the global input stability check for the selector.
* - `once` - Run only the first time the selector is called.
* - `always` - Run every time the selector is called.
* - `never` - Never run the input stability check.
*
* @default 'once'
*
* @see {@link https://reselect.js.org/api/development-only-stability-checks Development-Only Stability Checks}
* @see {@link https://reselect.js.org/api/development-only-stability-checks#inputstabilitycheck `inputStabilityCheck`}
* @see {@link https://reselect.js.org/api/development-only-stability-checks#2-per-selector-by-passing-an-inputstabilitycheck-option-directly-to- per-selector-configuration}
*
* @since 5.0.0
*/
inputStabilityCheck: DevModeCheckFrequency
/**
* Overrides the global identity function check for the selector.
* - `once` - Run only the first time the selector is called.
* - `always` - Run every time the selector is called.
* - `never` - Never run the identity function check.
*
* @default 'once'
*
* @see {@link https://reselect.js.org/api/development-only-stability-checks Development-Only Stability Checks}
* @see {@link https://reselect.js.org/api/development-only-stability-checks#identityfunctioncheck `identityFunctionCheck`}
* @see {@link https://reselect.js.org/api/development-only-stability-checks#2-per-selector-by-passing-an-identityfunctioncheck-option-directly-to- per-selector-configuration}
*
* @since 5.0.0
*/
identityFunctionCheck: DevModeCheckFrequency
}
/**
* Represents execution information for development mode checks.
*
* @public
* @since 5.0.0
*/
export type DevModeChecksExecutionInfo = {
[K in keyof DevModeChecks]: {
/**
* A boolean indicating whether the check should be executed.
*/
shouldRun: boolean
/**
* The function to execute for the check.
*/
run: AnyFunction
}
}
/**
* Determines the combined single "State" type (first arg) from all input selectors.
*
* @public
*/
export type GetStateFromSelectors<Selectors extends SelectorArray> =
MergeParameters<Selectors>[0]
/**
* Determines the combined "Params" type (all remaining args) from all input selectors.
*
* @public
*/
export type GetParamsFromSelectors<Selectors extends SelectorArray> = ArrayTail<
MergeParameters<Selectors>
>
/**
* Any Memoizer function. A memoizer is a function that accepts another function and returns it.
*
* @template FunctionType - The type of the function that is memoized.
*
* @public
*/
export type UnknownMemoizer<
FunctionType extends UnknownFunction = UnknownFunction
> = (func: FunctionType, ...options: any[]) => FunctionType
/**
* Extracts the options type for a memoization function based on its parameters.
* The first parameter of the function is expected to be the function to be memoized,
* followed by options for the memoization process.
*
* @template MemoizeFunction - The type of the memoize function to be checked.
*
* @public
*/
export type MemoizeOptionsFromParameters<
MemoizeFunction extends UnknownMemoizer
> =
| (
| NonFunctionType<DropFirstParameter<MemoizeFunction>[0]>
| FunctionType<DropFirstParameter<MemoizeFunction>[0]>
)
| (
| NonFunctionType<DropFirstParameter<MemoizeFunction>[number]>
| FunctionType<DropFirstParameter<MemoizeFunction>[number]>
)[]
/**
* Derive the type of memoize options object based on whether the memoize function itself was overridden.
*
* _This type can be used for both `memoizeOptions` and `argsMemoizeOptions`._
*
* @template MemoizeFunction - The type of the `memoize` or `argsMemoize` function initially passed into `createSelectorCreator`.
* @template OverrideMemoizeFunction - The type of the optional `memoize` or `argsMemoize` function passed directly into `createSelector` which then overrides the original `memoize` or `argsMemoize` function passed into `createSelectorCreator`.
*
* @public
*/
export type OverrideMemoizeOptions<
MemoizeFunction extends UnknownMemoizer,
OverrideMemoizeFunction extends UnknownMemoizer = never
> = IfNever<
OverrideMemoizeFunction,
Simplify<MemoizeOptionsFromParameters<MemoizeFunction>>,
Simplify<MemoizeOptionsFromParameters<OverrideMemoizeFunction>>
>
/**
* Extracts the additional properties or methods that a memoize function attaches to
* the function it memoizes (e.g., `clearCache`).
*
* @template MemoizeFunction - The type of the memoize function to be checked.
*
* @public
*/
export type ExtractMemoizerFields<MemoizeFunction extends UnknownMemoizer> =
Simplify<OmitIndexSignature<ReturnType<MemoizeFunction>>>
/**
* Represents the additional properties attached to a function memoized by `reselect`.
*
* `lruMemoize`, `weakMapMemoize` and `autotrackMemoize` all return these properties.
*
* @see {@linkcode ExtractMemoizerFields ExtractMemoizerFields}
*
* @public
*/
export type DefaultMemoizeFields = {
/**
* Clears the memoization cache associated with a memoized function.
* This method is typically used to reset the state of the cache, allowing
* for the garbage collection of previously memoized results and ensuring
* that future calls to the function recompute the results.
*/
clearCache: () => void
resultsCount: () => number
resetResultsCount: () => void
}
/*
* -----------------------------------------------------------------------------
* -----------------------------------------------------------------------------
*
* Reselect Internal Utility Types
*
* -----------------------------------------------------------------------------
* -----------------------------------------------------------------------------
*/
/**
* Any function with any arguments.
*
* @internal
*/
export type AnyFunction = (...args: any[]) => any
/**
* Any function with unknown arguments.
*
* @internal
*/
export type UnknownFunction = (...args: unknown[]) => unknown
/**
* When a generic type parameter is using its default value of `never`, fallback to a different type.
*
* @template T - Type to be checked.
* @template FallbackTo - Type to fallback to if `T` resolves to `never`.
*
* @internal
*/
export type FallbackIfNever<T, FallbackTo> = IfNever<T, FallbackTo, T>
/**
* Extracts the non-function part of a type.
*
* @template T - The input type to be refined by excluding function types and index signatures.
*
* @internal
*/
export type NonFunctionType<T> = Simplify<
OmitIndexSignature<Exclude<T, AnyFunction>>
>
/**
* Extracts the function part of a type.
*
* @template T - The input type to be refined by extracting function types.
*
* @internal
*/
export type FunctionType<T> = Extract<T, AnyFunction>
/**
* Extracts the return type from all functions as a tuple.
*
* @internal
*/
export type ExtractReturnType<FunctionsArray extends readonly AnyFunction[]> = {
[Index in keyof FunctionsArray]: FunctionsArray[Index] extends FunctionsArray[number]
? FallbackIfUnknown<ReturnType<FunctionsArray[Index]>, any>
: never
}
/**
* Utility type to infer the type of "all params of a function except the first",
* so we can determine what arguments a memoize function accepts.
*
* @internal
*/
export type DropFirstParameter<Func extends AnyFunction> = Func extends (
firstArg: any,
...restArgs: infer Rest
) => any
? Rest
: never
/**
* Distributes over a type. It is used mostly to expand a function type
* in hover previews while preserving their original JSDoc information.
*
* If preserving JSDoc information is not a concern, you can use {@linkcode ExpandFunction ExpandFunction}.
*
* @template T The type to be distributed.
*
* @internal
*/
export type Distribute<T> = T extends T ? T : never
/**
* Extracts the type of the first element of an array or tuple.
*
* @internal
*/
export type FirstArrayElement<ArrayType> = ArrayType extends readonly [
unknown,
...unknown[]
]
? ArrayType[0]
: never
/**
* Extracts the type of an array or tuple minus the first element.
*
* @internal
*/
export type ArrayTail<ArrayType> = ArrayType extends readonly [
unknown,
...infer Tail
]
? Tail
: []
/**
* An alias for type `{}`. Represents any value that is not `null` or `undefined`.
* It is mostly used for semantic purposes to help distinguish between an
* empty object type and `{}` as they are not the same.
*
* @internal
*/
export type AnyNonNullishValue = NonNullable<unknown>
/**
* Same as {@linkcode AnyNonNullishValue AnyNonNullishValue} but aliased
* for semantic purposes. It is intended to be used in scenarios where
* a recursive type definition needs to be interrupted to ensure type safety
* and to avoid excessively deep recursion that could lead to performance issues.
*
* @internal
*/
export type InterruptRecursion = AnyNonNullishValue
/*
* -----------------------------------------------------------------------------
* -----------------------------------------------------------------------------
*
* External/Copied Utility Types
*
* -----------------------------------------------------------------------------
* -----------------------------------------------------------------------------
*
*/
/**
* An if-else-like type that resolves depending on whether the given type is `never`.
* This is mainly used to conditionally resolve the type of a `memoizeOptions` object based on whether `memoize` is provided or not.
* @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/if-never.d.ts Source}
*
* @internal
*/
export type IfNever<T, TypeIfNever, TypeIfNotNever> = [T] extends [never]
? TypeIfNever
: TypeIfNotNever
/**
* Omit any index signatures from the given object type, leaving only explicitly defined properties.
* This is mainly used to remove explicit `any`s from the return type of some memoizers (e.g, `microMemoize`).
*
* __Disclaimer:__ When used on an intersection of a function and an object,
* the function is erased.
*
* @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/omit-index-signature.d.ts Source}
*
* @internal
*/
export type OmitIndexSignature<ObjectType> = {
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
? never
: KeyType]: ObjectType[KeyType]
}
/**
* The infamous "convert a union type to an intersection type" hack
* @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/union-to-intersection.d.ts Source}
* @see {@link https://github.com/microsoft/TypeScript/issues/29594 Reference}
*
* @internal
*/
export type UnionToIntersection<Union> =
// `extends unknown` is always going to be the case and is used to convert the
// `Union` into a [distributive conditional
// type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
(
Union extends unknown
? // The union type is used as the only argument to a function since the union
// of function arguments is an intersection.
(distributedUnion: Union) => void
: // This won't happen.
never
) extends // Infer the `Intersection` type since TypeScript represents the positional
// arguments of unions of functions as an intersection of the union.
(mergedIntersection: infer Intersection) => void
? // The `& Union` is to allow indexing by the resulting type
Intersection & Union
: never
/**
* Code to convert a union of values into a tuple.
* @see {@link https://stackoverflow.com/a/55128956/62937 Source}
*
* @internal
*/
type Push<T extends any[], V> = [...T, V]
/**
* @see {@link https://stackoverflow.com/a/55128956/62937 Source}
*
* @internal
*/
type LastOf<T> = UnionToIntersection<
T extends any ? () => T : never
> extends () => infer R
? R
: never
/**
* TS4.1+
* @see {@link https://stackoverflow.com/a/55128956/62937 Source}
*
* @internal
*/
export type TuplifyUnion<
T,
L = LastOf<T>,
N = [T] extends [never] ? true : false
> = true extends N ? [] : Push<TuplifyUnion<Exclude<T, L>>, L>
/**
* Converts "the values of an object" into a tuple, like a type-level `Object.values()`
* @see {@link https://stackoverflow.com/a/68695508/62937 Source}
*
* @internal
*/
export type ObjectValuesToTuple<
T,
KS extends any[] = TuplifyUnion<keyof T>,
R extends any[] = []
> = KS extends [infer K, ...infer KT]
? ObjectValuesToTuple<T, KT, [...R, T[K & keyof T]]>
: R
/**
* Create a type that makes the given keys required.
* The remaining keys are kept as is.
*
* @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/set-required.d.ts Source}
*
* @internal
*/
export type SetRequired<BaseType, Keys extends keyof BaseType> = Omit<
BaseType,
Keys
> &
Required<Pick<BaseType, Keys>>
/**
* An if-else-like type that resolves depending on whether the given type is `unknown`.
* @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/if-unknown.d.ts Source}
*
* @internal
*/
export type IfUnknown<T, TypeIfUnknown, TypeIfNotUnknown> = unknown extends T // `T` can be `unknown` or `any`
? [T] extends [null] // `any` can be `null`, but `unknown` can't be
? TypeIfNotUnknown
: TypeIfUnknown
: TypeIfNotUnknown
/**
* When a type is resolves to `unknown`, fallback to a different type.
*
* @template T - Type to be checked.
* @template FallbackTo - Type to fallback to if `T` resolves to `unknown`.
*
* @internal
*/
export type FallbackIfUnknown<T, FallbackTo> = IfUnknown<T, FallbackTo, T>
/**
*
* -----------------------------------------------------------------------------
* -----------------------------------------------------------------------------
*
* Type Expansion Utilities
*
* -----------------------------------------------------------------------------
* -----------------------------------------------------------------------------
*
*/
/**
* Check whether `U` contains `U1`.
* @see {@link https://millsp.github.io/ts-toolbelt/modules/union_has.html Source}
*
* @internal
*/
export type Has<U, U1> = [U1] extends [U] ? 1 : 0
/**
* @internal
*/
export type Boolean2 = 0 | 1
/**
* @internal
*/
export type If2<B extends Boolean2, Then, Else = never> = B extends 1
? Then
: Else
/**
* @internal
*/
export type BuiltIn =
| Function
| Error
| Date
| { readonly [Symbol.toStringTag]: string }
| RegExp
| Generator
/**
* Expand an item a single level.
* @see {@link https://stackoverflow.com/a/69288824/62937 Source}
*
* @internal
*/
export type Expand<T> = T extends (...args: infer A) => infer R
? (...args: Expand<A>) => Expand<R>
: T extends infer O
? { [K in keyof O]: O[K] }
: never
/**
* Expand an item recursively.
* @see {@link https://stackoverflow.com/a/69288824/62937 Source}
*
* @internal
*/
export type ExpandRecursively<T> = T extends (...args: infer A) => infer R
? (...args: ExpandRecursively<A>) => ExpandRecursively<R>
: T extends object
? T extends infer O
? { [K in keyof O]: ExpandRecursively<O[K]> }
: never
: T
/**
* @internal
*/
export type Identity<T> = T
/**
* Another form of type value expansion
* @see {@link https://github.com/microsoft/TypeScript/issues/35247 Source}
*
* @internal
*/
export type Mapped<T> = Identity<{ [k in keyof T]: T[k] }>
/**
* This utility type is primarily used to expand a function type in order to
* improve its visual display in hover previews within IDEs.
*
* __Disclaimer:__ Functions expanded using this type will not display their
* original JSDoc information in hover previews.
*
* @template FunctionType - The type of the function to be expanded.
*
* @internal
*/
export type ExpandFunction<FunctionType extends AnyFunction> =
FunctionType extends FunctionType
? (...args: Parameters<FunctionType>) => ReturnType<FunctionType>
: never
/**
* Useful to flatten the type output to improve type hints shown in editors.
* And also to transform an interface into a type to aide with assignability.
* @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/simplify.d.ts Source}
*
* @internal
*/
export type Simplify<T> = T extends AnyFunction
? T
: {
[KeyType in keyof T]: T[KeyType]
} & AnyNonNullishValue
/**
* Fully expand a type, deeply
* @see {@link https://github.com/millsp/ts-toolbelt Any.Compute}
*
* @internal
*/
export type ComputeDeep<A, Seen = never> = A extends BuiltIn
? A
: If2<
Has<Seen, A>,
A,
A extends any[]
? A extends Record<PropertyKey, any>[]
? ({
[K in keyof A[number]]: ComputeDeep<A[number][K], A | Seen>
} & unknown)[]
: A
: A extends readonly any[]
? A extends readonly Record<PropertyKey, any>[]
? readonly ({
[K in keyof A[number]]: ComputeDeep<A[number][K], A | Seen>
} & unknown)[]
: A
: { [K in keyof A]: ComputeDeep<A[K], A | Seen> } & unknown
>
|