Commit 727a8bb4 authored by Robert Knight's avatar Robert Knight

Extract helper types out of `create-store.ts`

Per feedback at
https://github.com/hypothesis/client/pull/4944#pullrequestreview-1178131884, try
to make `create-store.ts` easier to scan by moving some of the typing-related
utilities into a separate module.
parent 456d4722
......@@ -4,11 +4,7 @@ import * as redux from 'redux';
import thunk from 'redux-thunk';
import { immutable } from '../util/immutable';
/** Helper that strips the first argument from a function type. */
type OmitFirstArg<F> = F extends (x: any, ...args: infer P) => infer R
? (...args: P) => R
: never;
import type { OmitFirstArg, TupleToIntersection } from './type-utils';
/**
* Helper that converts an object of selector functions, which take a `state`
......@@ -116,21 +112,6 @@ function assignOnce<T extends object, U extends object>(target: T, source: U) {
return Object.assign(target, source);
}
type MapContravariant<T> = { [K in keyof T]: (x: T[K]) => void };
/**
* Utility that turns a tuple type `[A, B, C]` into an intersection `A & B & C`.
*
* The implementation is magic adapted from
* https://github.com/microsoft/TypeScript/issues/28323. Roughly speaking it
* works by computing a type that could be assigned to any position in the
* tuple, which must be the intersection of all the tuple element types.
*/
type TupleToIntersection<
T,
Temp extends Record<number, unknown> = MapContravariant<T>
> = Temp[number] extends (x: infer U) => unknown ? U : never;
/**
* Create a Redux store from a set of modules.
*
......
type MapContravariant<T> = { [K in keyof T]: (x: T[K]) => void };
/**
* Utility that turns a tuple type `[A, B, C]` into an intersection `A & B & C`.
*
* The implementation is magic adapted from
* https://github.com/microsoft/TypeScript/issues/28323. Roughly speaking it
* works by computing a type that could be assigned to any position in the
* tuple, which must be the intersection of all the tuple element types.
*/
export type TupleToIntersection<
T,
Temp extends Record<number, unknown> = MapContravariant<T>
> = Temp[number] extends (x: infer U) => unknown ? U : never;
/**
* Helper that strips the first argument from a function type.
*
* This maps a type like `(a: T1, b: T2, ...) => Result` to `(b: T2, ...) => Result`.
*/
export type OmitFirstArg<F> = F extends (x: any, ...args: infer P) => infer R
? (...args: P) => R
: never;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment