-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding types order, moved types to package (#52)
* adding types order, moved types to package * stop autogenerating types with microbuble with --no-generateTypes flag * Update package.json Co-authored-by: Aral Roca Gomez <[email protected]> * put initialState as optional at createStore * upgrade canary version * set now accepts function as param * fix param on function as set value * remove type for reset on hookreturn * update canary version * update canary version Co-authored-by: Aral Roca Gomez <[email protected]>
- Loading branch information
Showing
2 changed files
with
58 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
declare module "teaful" { | ||
|
||
import React from "react"; | ||
|
||
type HookReturn<T> = [T, (value: T | ((value: T) => T | undefined | null) ) => void]; | ||
type initialStoreType = Record<string, any>; | ||
|
||
type Hook<S> = ( | ||
initial?: S, | ||
onAfterUpdate?: afterCallbackType<S> | ||
) => HookReturn<S>; | ||
|
||
type HookDry<S> = (initial?: S) => HookReturn<S>; | ||
|
||
export type Hoc<S> = { store: HookReturn<S> }; | ||
|
||
type HocFunc<S, R extends React.ComponentClass = React.ComponentClass> = ( | ||
component: R, | ||
initial?: S | ||
) => R; | ||
|
||
type afterCallbackType<S extends initialStoreType> = (param: { | ||
store: S; | ||
prevStore: S; | ||
}) => void; | ||
|
||
type getStoreType<S extends initialStoreType> = { | ||
[key in keyof S | string ]: S[key] extends initialStoreType | ||
? useStoreType<S[key]> & HookDry<S[key]> : HookDry<S[key]>; | ||
}; | ||
|
||
type useStoreType<S extends initialStoreType> = { | ||
[key in keyof S]: S[key] extends initialStoreType | ||
? useStoreType<S[key]> & Hook<S[key]> : Hook<S[key]>; | ||
}; | ||
|
||
type withStoreType<S extends initialStoreType> = { | ||
[key in keyof S]: S[key] extends initialStoreType | ||
? withStoreType<S[key]> & HocFunc<S> | ||
: HocFunc<S>; | ||
}; | ||
|
||
function createStore<S extends initialStoreType>( | ||
initial?: S, | ||
afterCallback?: afterCallbackType<S> | ||
): { | ||
getStore: HookDry<S> & getStoreType<S>; | ||
useStore: Hook<S> & useStoreType<S>; | ||
withStore: HocFunc<S> & withStoreType<S>; | ||
}; | ||
|
||
export default createStore; | ||
} |