tsHelpers.d.ts 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import type { Middleware, StoreEnhancer } from 'redux';
  2. import type { EnhancerArray, MiddlewareArray } from './utils';
  3. /**
  4. * return True if T is `any`, otherwise return False
  5. * taken from https://github.com/joonhocho/tsdef
  6. *
  7. * @internal
  8. */
  9. export declare type IsAny<T, True, False = never> = true | false extends (T extends never ? true : false) ? True : False;
  10. /**
  11. * return True if T is `unknown`, otherwise return False
  12. * taken from https://github.com/joonhocho/tsdef
  13. *
  14. * @internal
  15. */
  16. export declare type IsUnknown<T, True, False = never> = unknown extends T ? IsAny<T, False, True> : False;
  17. export declare type FallbackIfUnknown<T, Fallback> = IsUnknown<T, Fallback, T>;
  18. /**
  19. * @internal
  20. */
  21. export declare type IfMaybeUndefined<P, True, False> = [undefined] extends [P] ? True : False;
  22. /**
  23. * @internal
  24. */
  25. export declare type IfVoid<P, True, False> = [void] extends [P] ? True : False;
  26. /**
  27. * @internal
  28. */
  29. export declare type IsEmptyObj<T, True, False = never> = T extends any ? keyof T extends never ? IsUnknown<T, False, IfMaybeUndefined<T, False, IfVoid<T, False, True>>> : False : never;
  30. /**
  31. * returns True if TS version is above 3.5, False if below.
  32. * uses feature detection to detect TS version >= 3.5
  33. * * versions below 3.5 will return `{}` for unresolvable interference
  34. * * versions above will return `unknown`
  35. *
  36. * @internal
  37. */
  38. export declare type AtLeastTS35<True, False> = [True, False][IsUnknown<ReturnType<(<T>() => T)>, 0, 1>];
  39. /**
  40. * @internal
  41. */
  42. export declare type IsUnknownOrNonInferrable<T, True, False> = AtLeastTS35<IsUnknown<T, True, False>, IsEmptyObj<T, True, IsUnknown<T, True, False>>>;
  43. /**
  44. * Convert a Union type `(A|B)` to an intersection type `(A&B)`
  45. */
  46. export declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
  47. export declare type ExcludeFromTuple<T, E, Acc extends unknown[] = []> = T extends [
  48. infer Head,
  49. ...infer Tail
  50. ] ? ExcludeFromTuple<Tail, E, [...Acc, ...([Head] extends [E] ? [] : [Head])]> : Acc;
  51. declare type ExtractDispatchFromMiddlewareTuple<MiddlewareTuple extends any[], Acc extends {}> = MiddlewareTuple extends [infer Head, ...infer Tail] ? ExtractDispatchFromMiddlewareTuple<Tail, Acc & (Head extends Middleware<infer D> ? IsAny<D, {}, D> : {})> : Acc;
  52. export declare type ExtractDispatchExtensions<M> = M extends MiddlewareArray<infer MiddlewareTuple> ? ExtractDispatchFromMiddlewareTuple<MiddlewareTuple, {}> : M extends ReadonlyArray<Middleware> ? ExtractDispatchFromMiddlewareTuple<[...M], {}> : never;
  53. declare type ExtractStoreExtensionsFromEnhancerTuple<EnhancerTuple extends any[], Acc extends {}> = EnhancerTuple extends [infer Head, ...infer Tail] ? ExtractStoreExtensionsFromEnhancerTuple<Tail, Acc & (Head extends StoreEnhancer<infer Ext> ? IsAny<Ext, {}, Ext> : {})> : Acc;
  54. export declare type ExtractStoreExtensions<E> = E extends EnhancerArray<infer EnhancerTuple> ? ExtractStoreExtensionsFromEnhancerTuple<EnhancerTuple, {}> : E extends ReadonlyArray<StoreEnhancer> ? UnionToIntersection<E[number] extends StoreEnhancer<infer Ext> ? Ext extends {} ? IsAny<Ext, {}, Ext> : {} : {}> : never;
  55. declare type ExtractStateExtensionsFromEnhancerTuple<EnhancerTuple extends any[], Acc extends {}> = EnhancerTuple extends [infer Head, ...infer Tail] ? ExtractStateExtensionsFromEnhancerTuple<Tail, Acc & (Head extends StoreEnhancer<any, infer StateExt> ? IsAny<StateExt, {}, StateExt> : {})> : Acc;
  56. export declare type ExtractStateExtensions<E> = E extends EnhancerArray<infer EnhancerTuple> ? ExtractStateExtensionsFromEnhancerTuple<EnhancerTuple, {}> : E extends ReadonlyArray<StoreEnhancer> ? UnionToIntersection<E[number] extends StoreEnhancer<any, infer StateExt> ? StateExt extends {} ? IsAny<StateExt, {}, StateExt> : {} : {}> : never;
  57. /**
  58. * Helper type. Passes T out again, but boxes it in a way that it cannot
  59. * "widen" the type by accident if it is a generic that should be inferred
  60. * from elsewhere.
  61. *
  62. * @internal
  63. */
  64. export declare type NoInfer<T> = [T][T extends any ? 0 : never];
  65. export declare type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
  66. export interface TypeGuard<T> {
  67. (value: any): value is T;
  68. }
  69. export interface HasMatchFunction<T> {
  70. match: TypeGuard<T>;
  71. }
  72. export declare const hasMatchFunction: <T>(v: Matcher<T>) => v is HasMatchFunction<T>;
  73. /** @public */
  74. export declare type Matcher<T> = HasMatchFunction<T> | TypeGuard<T>;
  75. /** @public */
  76. export declare type ActionFromMatcher<M extends Matcher<any>> = M extends Matcher<infer T> ? T : never;
  77. export declare type Id<T> = {
  78. [K in keyof T]: T[K];
  79. } & {};
  80. export {};