Skip to content

Instantly share code, notes, and snippets.

@jednano
Created September 4, 2019 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jednano/c38fb9cf99510b0bc79cbc1de6dcc246 to your computer and use it in GitHub Desktop.
Save jednano/c38fb9cf99510b0bc79cbc1de6dcc246 to your computer and use it in GitHub Desktop.
Redux TS
/** Essentials */
export type Primitive = string | number | boolean | bigint | symbol | undefined | null;
/** Like Readonly but recursive */
export type DeepReadonly<T> = T extends Primitive
? T
: T extends Function
? T
: T extends Date
? T
: T extends Map<infer K, infer V>
? ReadonlyMap<K, V>
: T extends Set<infer U>
? ReadonlySet<U>
: T extends {}
? { readonly [K in keyof T]: DeepReadonly<T[K]> }
: Readonly<T>;
interface ReadonlySet<ItemType> extends Set<DeepReadonly<ItemType>> {}
interface ReadonlyMap<KeyType, ValueType> extends Map<DeepReadonly<KeyType>, DeepReadonly<ValueType>> {}
/** Merge 2 types, properties types from the latter override the ones defined on the former type */
export type Merge<M, N> = Omit<M, Extract<keyof M, keyof N>> & N;
export interface Action<T extends string> {
type: T
}
export class Store<S, A extends Action<any>> {
private _state: S
constructor(private reducer: (action: A, state?: DeepReadonly<S>) => S) {
this._state = reducer({} as A)
}
public get state() {
return this._state as DeepReadonly<S>
}
public dispatch<T extends A>(action: T) {
this._state = this.reducer(action, this.state)
}
}
export function applyMiddleware<T>(...middlewares: T[]) {
return () => (...args: ConstructorParameters<typeof Store>) => {
const store = new Store(...args)
const dispatch = () => {
throw new Error('not allowed')
}
const middlewareAPI = {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment