Skip to content

Commit

Permalink
adding types order, moved types to package
Browse files Browse the repository at this point in the history
  • Loading branch information
danielart committed Nov 25, 2021
1 parent bc6a297 commit ff147c2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
}
]
},
"types": "dist/index.d.ts",
"types": "package/index.d.ts",
"scripts": {
"lint": "eslint ./package ./tests",
"format": "eslint --fix ./package ./tests ./examples",
Expand Down
53 changes: 53 additions & 0 deletions package/index.d.ts
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;
}

0 comments on commit ff147c2

Please sign in to comment.