Skip to content

Commit

Permalink
adding types order, moved types to package (#52)
Browse files Browse the repository at this point in the history
* 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
danielart and aralroca authored Dec 2, 2021
1 parent 6a00142 commit b70f074
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "teaful",
"version": "0.8.1",
"version": "0.9.0-canary.7",
"description": "Tiny, easy and powerful React state management (less than 1kb)",
"license": "MIT",
"keywords": [
Expand Down Expand Up @@ -51,15 +51,16 @@
}
]
},
"types": "dist/index.d.ts",
"types": "package/index.d.ts",
"scripts": {
"lint": "eslint ./package ./tests",
"format": "eslint --fix ./package ./tests ./examples",
"test": "jest ./package ./tests",
"test:example:todo-list": "jest ./examples/todo-list",
"test:examples": "jest ./examples",
"test:watch": "jest ./package ./tests --watch",
"build": "microbundle --jsx React.createElement",
"build-types": "cp ./package/index.d.ts ./dist/index.d.ts",
"build": "microbundle --jsx React.createElement --no-generateTypes && yarn build-types",
"dev": "microbundle watch",
"prepublish": "yarn build"
},
Expand All @@ -68,7 +69,7 @@
},
"jest": {
"testEnvironment": "jsdom",
"moduleNameMapper": {
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(css|less)$": "identity-obj-proxy"
}
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 | ((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;
}

0 comments on commit b70f074

Please sign in to comment.