From 27993fcadf75870cf7b7f7b5bbc8cdebb5e64ec5 Mon Sep 17 00:00:00 2001 From: paldepind Date: Sat, 17 Dec 2016 14:01:58 +0100 Subject: [PATCH] Typing for module hooks and `h` data --- package.json | 2 +- src/h.ts | 10 +++++----- src/modules/module.d.ts | 12 ++++++------ src/snabbdom.ts | 20 +++++++++++++++----- src/vnode.ts | 8 ++++---- 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 269e48f..7cf4c2e 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "gulp-uglify": "^1.5.3", "knuth-shuffle": "^1.0.1", "testem": "^1.0.2", - "typescript": "^2.0.6", + "typescript": "^2.1.0", "xyz": "0.5.x" }, "scripts": { diff --git a/src/h.ts b/src/h.ts index 3e0a3f8..1335bef 100644 --- a/src/h.ts +++ b/src/h.ts @@ -1,4 +1,4 @@ -import {vnode, VNode} from './vnode'; +import {vnode, VNode, VNodeData} from './vnode'; import * as is from './is'; function addNS(data: any, children: Array | undefined, sel: string | undefined): void { @@ -11,13 +11,13 @@ function addNS(data: any, children: Array | undefined, sel: string | unde } export function h(sel: string): VNode; -export function h(sel: string, data: any): VNode; +export function h(sel: string, data: VNodeData): VNode; export function h(sel: string, text: string): VNode; export function h(sel: string, children: Array): VNode; -export function h(sel: string, data: any, text: string): VNode; -export function h(sel: string, data: any, children: Array): VNode; +export function h(sel: string, data: VNodeData, text: string): VNode; +export function h(sel: string, data: VNodeData, children: Array): VNode; export function h(sel: any, b?: any, c?: any): VNode { - var data = {}, children: any, text: any, i: number; + var data: VNodeData = {}, children: any, text: any, i: number; if (c !== undefined) { data = b; if (is.array(c)) { children = c; } diff --git a/src/modules/module.d.ts b/src/modules/module.d.ts index eab4951..256d8ba 100644 --- a/src/modules/module.d.ts +++ b/src/modules/module.d.ts @@ -1,10 +1,10 @@ import {PreHook, CreateHook, UpdateHook, DestroyHook, RemoveHook, PostHook} from '../hooks'; export interface Module { - pre?: PreHook; - create?: CreateHook; - update?: UpdateHook; - destroy?: DestroyHook; - remove?: RemoveHook; - post?: PostHook; + pre: PreHook; + create: CreateHook; + update: UpdateHook; + destroy: DestroyHook; + remove: RemoveHook; + post: PostHook; } \ No newline at end of file diff --git a/src/snabbdom.ts b/src/snabbdom.ts index 0f0d8a1..e89b66b 100644 --- a/src/snabbdom.ts +++ b/src/snabbdom.ts @@ -1,4 +1,5 @@ /* global module, document, Node */ +import {Module} from './modules/module'; import {Hooks} from './hooks'; import vnode, {VNode, VNodeData, Key} from './vnode'; import * as is from './is'; @@ -21,8 +22,14 @@ function isVnode(vnode: any): vnode is VNode { type KeyToIndexMap = {[key: string]: number}; +type ArraysOf = { + [K in keyof T]: (T[K])[]; +} + +type ModuleHooks = ArraysOf; + function createKeyToOldIdx(children: Array, beginIdx: number, endIdx: number): KeyToIndexMap { - let i: number, map: {[s: string]: number} = {}, key: Key; + let i: number, map: KeyToIndexMap = {}, key: Key; for (i = beginIdx; i <= endIdx; ++i) { key = children[i].key; if (key !== undefined) map[key] = i; @@ -30,20 +37,23 @@ function createKeyToOldIdx(children: Array, beginIdx: number, endIdx: num return map; } -const hooks = ['create', 'update', 'remove', 'destroy', 'pre', 'post']; +const hooks: (keyof Module)[] = ['create', 'update', 'remove', 'destroy', 'pre', 'post']; export {h} from './h'; export {thunk} from './thunk'; -export function init(modules: Array, domApi?: DOMAPI) { - let i: number, j: number, cbs: any = {}; +export function init(modules: Array>, domApi?: DOMAPI) { + let i: number, j: number, cbs = ({} as ModuleHooks); const api: DOMAPI = domApi !== undefined ? domApi : htmlDomApi; for (i = 0; i < hooks.length; ++i) { cbs[hooks[i]] = []; for (j = 0; j < modules.length; ++j) { - if ((modules[j] as any)[hooks[i]] !== undefined) cbs[hooks[i]].push((modules[j] as any)[hooks[i]]); + const hook = modules[j][hooks[i]]; + if (hook !== undefined) { + (cbs[hooks[i]] as Array).push(hook); + } } } diff --git a/src/vnode.ts b/src/vnode.ts index d9055d8..ba7c962 100644 --- a/src/vnode.ts +++ b/src/vnode.ts @@ -1,6 +1,6 @@ import {Hooks} from './hooks'; -export type Key = string | number | undefined; +export type Key = string | number; export interface VNode { sel: string | undefined; @@ -21,13 +21,13 @@ export interface VNodeData { on?: any; hero?: any; attachData?: any; - [key: string]: any; // for any other 3rd party module - // end of modules hook?: Hooks; - key?: string | number; + key?: Key; ns?: string; // for SVGs fn?: () => VNode; // for thunks args?: Array; // for thunks + [key: string]: any; // for any other 3rd party module + // end of modules } export function vnode(sel: string,