Skip to content

Commit

Permalink
fix type declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
huang2002 committed Jan 24, 2019
1 parent 00a6c9e commit 0533d59
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 29 deletions.
28 changes: 12 additions & 16 deletions src/core/HNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { EventMap } from "./events";

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

export type HProps<P extends object = any, DP extends Partial<P> = {}> = {
[K in keyof P]-?: K extends 'children' ? (
export type HProps<P extends object = any> = {
[K in keyof P]: K extends 'children' ? (
P extends { children: any } ? (
ArrayWrapped<P['children']>
) : (
Expand All @@ -21,33 +21,29 @@ export type HProps<P extends object = any, DP extends Partial<P> = {}> = {
)
)
) : (
K extends keyof DP ? (
Exclude<P[K], undefined>
) : (
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;
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>;
defaultStore?: Partial<S>;
storeHandlers?: PartialHandlers<SH, Store<S>>;
state?: Array<keyof S>;
context?: Array<keyof C>;
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;
init?: (this: HNode<P, S, C, SH, CH>, props: HProps<P>, store: Store<S, SH>, context: Store<C, CH>) => void;
render: (this: HNode<P, S, C, SH, CH>, props: HProps<P>, store: Store<S, SH>, context: Store<C, CH>) => unknown;
clear?: (this: HNode<P, S, C, SH, CH>, props: HProps<P>, store: Store<S, SH>, context: Store<C, CH>) => void;
catch?: (this: HNode<P, S, C, SH, CH>, err: any, props: HProps<P>, 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, DP extends Partial<P> = {}> {
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> {
isHN: true;
type: unknown;
desc?: HDesc<P, S, C, SH, CH, DP>;
props: HProps<P, DP>;
desc?: HDesc<P, S, C, SH, CH>;
props: HProps<P>;
sto?: Store<S, SH>;
ctx?: Store<C, CH>;
owner?: HNode<any>;
Expand Down
6 changes: 3 additions & 3 deletions src/core/HUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, SH extends HandlerMap<S> = any, CH extends HandlerMap<C> = any, DP extends Partial<P> = {}>(
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>(
type: HType<P, S, C, SH, CH> | string, props?: P | null, ...children: unknown[]
): HNode<P, S, C, SH, CH, DP> => ({
): HNode<P, S, C, SH, CH> => ({
isHN: true,
type,
desc: registry.get(type),
props: _assign({ children: children.flat(_Infinity) }, props) as unknown as HProps<P, DP>,
props: _assign({ children: children.flat(_Infinity) }, props) as unknown as HProps<P>,
active: true
});

Expand Down
7 changes: 3 additions & 4 deletions src/core/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ 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, any, any, any, any> | string, HDesc<any, any, any, any, any, any>>();
export const registry = new _Map<HType<any> | string, HDesc<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,
DP extends Partial<P> = {}
>(name: string, desc: HDesc<P, S, C, SH, CH, DP>): HType<P, S, C, SH, CH> {
CH extends HandlerMap<C> = any
>(name: string, desc: HDesc<P, S, C, SH, CH>): HType<P, S, C, SH, CH> {

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

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

interface TimerDefaultProps {
start: number;
}

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

state: ['time'],

Expand All @@ -32,7 +28,7 @@ const Timer = HUI.define<TimerProps, TimerStore, {}, TimerStoreHandlers, {}, Tim
},

init(props, store) {
store.set('time', props.start);
store.set('time', props.start!);
store.set('timer', store.trigger('setInterval', 1000));
},

Expand Down

0 comments on commit 0533d59

Please sign in to comment.