Skip to content

Commit

Permalink
improve type declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
huang2002 committed Jan 24, 2019
1 parent 41ab8b9 commit 17c00c5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 28 deletions.
50 changes: 31 additions & 19 deletions src/core/HNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,45 @@ import { EventMap } from "./events";

type ArrayWrapped<T> = T extends any[] ? T : [T];

export type HProps<P extends object = any> = Required<{
[K in keyof P]: K extends 'children' ?
/**/P extends { children: any } ?
/******/ArrayWrapped<P['children']> :
/******/P extends { children?: any } ?
/**********/ArrayWrapped<P['children']> | undefined :
/**********/unknown[] :
/**/P[K];
}> & ('children' extends keyof P ? {} : { children: unknown[] });

export interface HDesc<P extends object = any, S extends object = any, C extends object = any, SH extends HandlerMap<S> = any, CH extends HandlerMap<C> = any> {
defaultProps?: Partial<P>;
export type HProps<P extends object = any, DP extends Partial<P> = {}> = {
[K in keyof P]-?: K extends 'children' ? (
P extends { children: any } ? (
ArrayWrapped<P['children']>
) : (
P extends { children?: any } ? (
ArrayWrapped<P['children']> | undefined
) : (
unknown[]
)
)
) : (
K extends keyof DP ? (
Exclude<P[K], undefined>
) : (
P[K] | undefined
)
);
} & ('children' extends keyof P ? {} : {
children: unknown[];
});

export interface HDesc<P extends object = any, S extends object = any, C extends object = any, SH extends HandlerMap<S> = any, CH extends HandlerMap<C> = any, DP extends Partial<P> = {}> {
defaultProps?: DP;
defaultStore?: Partial<S>;
storeHandlers?: PartialHandlers<SH, Store<S>>;
state?: Array<keyof S>;
context?: Array<keyof C>;
init?: (this: HNode<P, S, C>, props: HProps<P>, store: Store<S, SH>, context: Store<C, CH>) => void;
render: (this: HNode<P, S, C>, props: HProps<P>, store: Store<S, SH>, context: Store<C, CH>) => unknown;
clear?: (this: HNode<P, S, C>, props: HProps<P>, store: Store<S, SH>, context: Store<C, CH>) => void;
catch?: (this: HNode<P, S, C>, err: any, props: HProps<P>, store: Store<S, SH>, context: Store<C, CH>) => unknown;
init?: (this: HNode<P, S, C, SH, CH, DP>, props: HProps<P, DP>, store: Store<S, SH>, context: Store<C, CH>) => void;
render: (this: HNode<P, S, C, SH, CH, DP>, props: HProps<P, DP>, store: Store<S, SH>, context: Store<C, CH>) => unknown;
clear?: (this: HNode<P, S, C, SH, CH, DP>, props: HProps<P, DP>, store: Store<S, SH>, context: Store<C, CH>) => void;
catch?: (this: HNode<P, S, C, SH, CH, DP>, err: any, props: HProps<P, DP>, store: Store<S, SH>, context: Store<C, CH>) => unknown;
}

export interface HNode<P extends object = EleProps, S extends object = any, C extends object = any, SH extends HandlerMap<S> = any, CH extends HandlerMap<C> = any> {
export interface HNode<P extends object = EleProps, S extends object = any, C extends object = any, SH extends HandlerMap<S> = any, CH extends HandlerMap<C> = any, DP extends Partial<P> = {}> {
isHN: true;
type: unknown;
desc?: HDesc<P, S, C, SH, CH>;
props: HProps<P>;
desc?: HDesc<P, S, C, SH, CH, DP>;
props: HProps<P, DP>;
sto?: Store<S, SH>;
ctx?: Store<C, CH>;
owner?: HNode<any>;
Expand Down
10 changes: 5 additions & 5 deletions src/core/HUI.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HNode, HProps } from "./HNode";
import { registry, define, HType } from "./registry";
import { _assign, _Infinity, _requestAnimationFrame } from "../utils/refCache";
import { createStore } from "./Store";
import { createStore, HandlerMap } from "./Store";
import { renderToDOM } from "./render";
import { propHandlers, EleProps } from "./propHandlers";
import { Portal } from "../ext/Portal";
Expand All @@ -10,13 +10,13 @@ import { compare } from "../utils/cmp";
import { Fragment } from "../ext/Fragment";
import { noCmpProps } from "../ticker/patch";

export const HUI = <P extends object = EleProps, S extends object = any, C extends object = any>(
type: HType<P, S, C> | string, props?: P | null, ...children: unknown[]
): HNode<P, S, C> => ({
export const HUI = <P extends object = EleProps, S extends object = any, C extends object = any, SH extends HandlerMap<S> = any, CH extends HandlerMap<C> = any, DP extends Partial<P> = {}>(
type: HType<P, S, C, SH, CH> | string, props?: P | null, ...children: unknown[]
): HNode<P, S, C, SH, CH, DP> => ({
isHN: true,
type,
desc: registry.get(type),
props: _assign({ children: children.flat(_Infinity) }, props) as HProps<P>,
props: _assign({ children: children.flat(_Infinity) }, props) as unknown as HProps<P, DP>,
active: true
});

Expand Down
7 changes: 4 additions & 3 deletions src/core/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ export interface HType<P extends object = any, S extends object = any, C extends
(props: P, store: Store<S, SH>, context: Store<C, CH>): any;
};

export const registry = new _Map<HType<any> | string, HDesc<any>>();
export const registry = new _Map<HType<any, any, any, any, any> | string, HDesc<any, any, any, any, any, any>>();

export const define = function <
P extends object = any,
S extends object = any,
C extends object = any,
SH extends HandlerMap<S> = any,
CH extends HandlerMap<C> = any
>(name: string, desc: HDesc<P, S, C, SH, CH>): HType<P, S, C, SH, CH> {
CH extends HandlerMap<C> = any,
DP extends Partial<P> = {}
>(name: string, desc: HDesc<P, S, C, SH, CH, DP>): HType<P, S, C, SH, CH> {

const type = _Symbol(name) as unknown as HType<P, S, C, SH, CH>;

Expand Down
6 changes: 5 additions & 1 deletion test/src/Timer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ type TimerStoreHandlers = {
setInterval: (interval: number) => any;
}

const Timer = HUI.define<TimerProps, TimerStore, {}, TimerStoreHandlers>('Timer', {
interface TimerDefaultProps {
start: number;
}

const Timer = HUI.define<TimerProps, TimerStore, {}, TimerStoreHandlers, {}, TimerDefaultProps>('Timer', {

state: ['time'],

Expand Down

0 comments on commit 17c00c5

Please sign in to comment.