From 17c00c56fe371271f671dfdff82f4a5f19019ebe Mon Sep 17 00:00:00 2001 From: hhh <1340545944@qq.com> Date: Thu, 24 Jan 2019 20:05:00 +0800 Subject: [PATCH] improve type declarations --- src/core/HNode.ts | 50 +++++++++++++++++++++++++++----------------- src/core/HUI.ts | 10 ++++----- src/core/registry.ts | 7 ++++--- test/src/Timer.tsx | 6 +++++- 4 files changed, 45 insertions(+), 28 deletions(-) diff --git a/src/core/HNode.ts b/src/core/HNode.ts index 35adc0f..1dd8df4 100644 --- a/src/core/HNode.ts +++ b/src/core/HNode.ts @@ -9,33 +9,45 @@ import { EventMap } from "./events"; type ArrayWrapped = T extends any[] ? T : [T]; -export type HProps

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

= any, CH extends HandlerMap = any> { - defaultProps?: Partial

; +export type HProps

= {}> = { + [K in keyof P]-?: K extends 'children' ? ( + P extends { children: any } ? ( + ArrayWrapped + ) : ( + P extends { children?: any } ? ( + ArrayWrapped | undefined + ) : ( + unknown[] + ) + ) + ) : ( + K extends keyof DP ? ( + Exclude + ) : ( + P[K] | undefined + ) + ); +} & ('children' extends keyof P ? {} : { + children: unknown[]; +}); + +export interface HDesc

= any, CH extends HandlerMap = any, DP extends Partial

= {}> { + defaultProps?: DP; defaultStore?: Partial; storeHandlers?: PartialHandlers>; state?: Array; context?: Array; - init?: (this: HNode, props: HProps

, store: Store, context: Store) => void; - render: (this: HNode, props: HProps

, store: Store, context: Store) => unknown; - clear?: (this: HNode, props: HProps

, store: Store, context: Store) => void; - catch?: (this: HNode, err: any, props: HProps

, store: Store, context: Store) => unknown; + init?: (this: HNode, props: HProps, store: Store, context: Store) => void; + render: (this: HNode, props: HProps, store: Store, context: Store) => unknown; + clear?: (this: HNode, props: HProps, store: Store, context: Store) => void; + catch?: (this: HNode, err: any, props: HProps, store: Store, context: Store) => unknown; } -export interface HNode

= any, CH extends HandlerMap = any> { +export interface HNode

= any, CH extends HandlerMap = any, DP extends Partial

= {}> { isHN: true; type: unknown; - desc?: HDesc; - props: HProps

; + desc?: HDesc; + props: HProps; sto?: Store; ctx?: Store; owner?: HNode; diff --git a/src/core/HUI.ts b/src/core/HUI.ts index 4af0b20..72d16d4 100644 --- a/src/core/HUI.ts +++ b/src/core/HUI.ts @@ -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"; @@ -10,13 +10,13 @@ import { compare } from "../utils/cmp"; import { Fragment } from "../ext/Fragment"; import { noCmpProps } from "../ticker/patch"; -export const HUI =

( - type: HType | string, props?: P | null, ...children: unknown[] -): HNode => ({ +export const HUI =

= any, CH extends HandlerMap = any, DP extends Partial

= {}>( + type: HType | string, props?: P | null, ...children: unknown[] +): HNode => ({ isHN: true, type, desc: registry.get(type), - props: _assign({ children: children.flat(_Infinity) }, props) as HProps

, + props: _assign({ children: children.flat(_Infinity) }, props) as unknown as HProps, active: true }); diff --git a/src/core/registry.ts b/src/core/registry.ts index 49aec67..f225d6b 100644 --- a/src/core/registry.ts +++ b/src/core/registry.ts @@ -6,15 +6,16 @@ export interface HType

, context: Store): any; }; -export const registry = new _Map | string, HDesc>(); +export const registry = new _Map | string, HDesc>(); export const define = function < P extends object = any, S extends object = any, C extends object = any, SH extends HandlerMap = any, - CH extends HandlerMap = any ->(name: string, desc: HDesc): HType { + CH extends HandlerMap = any, + DP extends Partial

= {} +>(name: string, desc: HDesc): HType { const type = _Symbol(name) as unknown as HType; diff --git a/test/src/Timer.tsx b/test/src/Timer.tsx index 65e8cb5..b8bf185 100644 --- a/test/src/Timer.tsx +++ b/test/src/Timer.tsx @@ -11,7 +11,11 @@ type TimerStoreHandlers = { setInterval: (interval: number) => any; } -const Timer = HUI.define('Timer', { +interface TimerDefaultProps { + start: number; +} + +const Timer = HUI.define('Timer', { state: ['time'],