diff --git a/package.json b/package.json index fdf71c6..4b54fd2 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/package/index.d.ts b/package/index.d.ts new file mode 100644 index 0000000..e6fd7f3 --- /dev/null +++ b/package/index.d.ts @@ -0,0 +1,53 @@ +declare module "teaful" { + + import React from "react"; + + type HookReturn = [T, (value: T) => void, () => void]; + type initialStoreType = Record; + + type Hook = ( + initial?: S, + onAfterUpdate?: afterCallbackType + ) => HookReturn; + + type HookDry = (initial?: S) => HookReturn; + + export type Hoc = { store: HookReturn }; + + type HocFunc = ( + component: R, + initial?: S + ) => R; + + type afterCallbackType = (param: { + store: S; + prevStore: S; + }) => void; + + type getStoreType = { + [key in keyof S]: S[key] extends initialStoreType + ? useStoreType & HookDry : HookDry; + }; + + type useStoreType = { + [key in keyof S]: S[key] extends initialStoreType + ? useStoreType & Hook : Hook; + }; + + type withStoreType = { + [key in keyof S]: S[key] extends initialStoreType + ? withStoreType & HocFunc + : HocFunc; + }; + + function createStore( + initial: S, + afterCallback?: afterCallbackType + ): { + getStore: HookDry & getStoreType; + useStore: Hook & useStoreType; + withStore: HocFunc & withStoreType; + }; + + export default createStore; +}