Skip to content

Commit b70f074

Browse files
danielartaralroca
andauthored
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 <aral-rg@hotmail.com> * 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 <aral-rg@hotmail.com>
1 parent 6a00142 commit b70f074

2 files changed

Lines changed: 58 additions & 4 deletions

File tree

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "teaful",
3-
"version": "0.8.1",
3+
"version": "0.9.0-canary.7",
44
"description": "Tiny, easy and powerful React state management (less than 1kb)",
55
"license": "MIT",
66
"keywords": [
@@ -51,15 +51,16 @@
5151
}
5252
]
5353
},
54-
"types": "dist/index.d.ts",
54+
"types": "package/index.d.ts",
5555
"scripts": {
5656
"lint": "eslint ./package ./tests",
5757
"format": "eslint --fix ./package ./tests ./examples",
5858
"test": "jest ./package ./tests",
5959
"test:example:todo-list": "jest ./examples/todo-list",
6060
"test:examples": "jest ./examples",
6161
"test:watch": "jest ./package ./tests --watch",
62-
"build": "microbundle --jsx React.createElement",
62+
"build-types": "cp ./package/index.d.ts ./dist/index.d.ts",
63+
"build": "microbundle --jsx React.createElement --no-generateTypes && yarn build-types",
6364
"dev": "microbundle watch",
6465
"prepublish": "yarn build"
6566
},
@@ -68,7 +69,7 @@
6869
},
6970
"jest": {
7071
"testEnvironment": "jsdom",
71-
"moduleNameMapper": {
72+
"moduleNameMapper": {
7273
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
7374
"\\.(css|less)$": "identity-obj-proxy"
7475
}

package/index.d.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
declare module "teaful" {
2+
3+
import React from "react";
4+
5+
type HookReturn<T> = [T, (value: T | ((value: T) => T | undefined | null) ) => void];
6+
type initialStoreType = Record<string, any>;
7+
8+
type Hook<S> = (
9+
initial?: S,
10+
onAfterUpdate?: afterCallbackType<S>
11+
) => HookReturn<S>;
12+
13+
type HookDry<S> = (initial?: S) => HookReturn<S>;
14+
15+
export type Hoc<S> = { store: HookReturn<S> };
16+
17+
type HocFunc<S, R extends React.ComponentClass = React.ComponentClass> = (
18+
component: R,
19+
initial?: S
20+
) => R;
21+
22+
type afterCallbackType<S extends initialStoreType> = (param: {
23+
store: S;
24+
prevStore: S;
25+
}) => void;
26+
27+
type getStoreType<S extends initialStoreType> = {
28+
[key in keyof S | string ]: S[key] extends initialStoreType
29+
? useStoreType<S[key]> & HookDry<S[key]> : HookDry<S[key]>;
30+
};
31+
32+
type useStoreType<S extends initialStoreType> = {
33+
[key in keyof S]: S[key] extends initialStoreType
34+
? useStoreType<S[key]> & Hook<S[key]> : Hook<S[key]>;
35+
};
36+
37+
type withStoreType<S extends initialStoreType> = {
38+
[key in keyof S]: S[key] extends initialStoreType
39+
? withStoreType<S[key]> & HocFunc<S>
40+
: HocFunc<S>;
41+
};
42+
43+
function createStore<S extends initialStoreType>(
44+
initial?: S,
45+
afterCallback?: afterCallbackType<S>
46+
): {
47+
getStore: HookDry<S> & getStoreType<S>;
48+
useStore: Hook<S> & useStoreType<S>;
49+
withStore: HocFunc<S> & withStoreType<S>;
50+
};
51+
52+
export default createStore;
53+
}

0 commit comments

Comments
 (0)