-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: deep immutable reducer type state
closes #3
- Loading branch information
Mohammad Hasani
committed
Feb 14, 2019
1 parent
35372da
commit de98e30
Showing
2 changed files
with
27 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,27 @@ | ||
type Primitive = undefined | null | boolean | string | number | Function | ||
|
||
interface DeepImmutableArray<T> extends ReadonlyArray<DeepImmutable<T>> {} | ||
interface DeepImmutableMap<K, V> | ||
extends ReadonlyMap<DeepImmutable<K>, DeepImmutable<V>> {} | ||
type DeepImmutableObject<T> = { readonly [K in keyof T]: DeepImmutable<T[K]> } | ||
|
||
export type Immutable<T> = T extends Primitive | ||
? T | ||
: T extends Array<infer U> | ||
? ReadonlyArray<U> | ||
: T extends Map<infer K, infer V> | ||
? ReadonlyMap<K, V> | ||
: Readonly<T> | ||
|
||
export type DeepImmutable<T> = T extends Primitive | ||
? T | ||
: T extends Array<infer U> | ||
? DeepImmutableArray<U> | ||
: T extends Map<infer K, infer V> | ||
? DeepImmutableMap<K, V> | ||
: DeepImmutableObject<T> | ||
|
||
export type Reducer<State, Actions> = ( | ||
prevState: State, | ||
prevState: DeepImmutable<State>, | ||
action: Actions | ||
) => State | ||
) => DeepImmutable<State> | State |