-
-
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
- Loading branch information
Showing
2 changed files
with
54 additions
and
1 deletion.
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) => void, () => 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]: 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; | ||
} |