From 492eb2ed67adb59b76f5cb9f6c5ad652c91cbff5 Mon Sep 17 00:00:00 2001 From: Chris Bowdon Date: Wed, 11 Feb 2015 20:51:48 +0000 Subject: [PATCH] Stricter TypeScript definitions --- mithril.d.ts | 194 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 140 insertions(+), 54 deletions(-) diff --git a/mithril.d.ts b/mithril.d.ts index 5bdcff5b..3279b8cc 100644 --- a/mithril.d.ts +++ b/mithril.d.ts @@ -1,76 +1,162 @@ //Mithril type definitions for Typescript interface MithrilStatic { - (selector: string, attributes: Object, children?: any): MithrilVirtualElement; - (selector: string, children?: any): MithrilVirtualElement; - prop(value?: any): (value?: any) => any; - withAttr(property: string, callback: (value: any) => void): (e: Event) => any; - module(rootElement: Node, module?: MithrilModule): Object; - trust(html: string): String; - render(rootElement: Element, children?: any): void; - render(rootElement: HTMLDocument, children?: any): void; - redraw: { - (): void; - strategy(key: string); - }; - route: { - (rootElement:Element, defaultRoute:string, routes:{ [key: string]: MithrilModule }): void - (element: Element, isInitialized: boolean): void; - (rootElement:HTMLDocument, defaultRoute:string, routes:{ [key: string]: MithrilModule }): void; - (path:string, params?:any, shouldReplaceHistory?:boolean): void; - (): string; - param(key:string): string; - mode: string; - }; - request(options: MithrilXHROptions): MithrilPromise; - deferred(): MithrilDeferred; - sync(promises: MithrilPromise[]): MithrilPromise; - startComputation(): void; - endComputation(): void; - deps(Object: any): Object; + + (selector: string, attributes: MithrilAttributes, ...children: Array): MithrilVirtualElement; + (selector: string, ...children: Array): MithrilVirtualElement; + + prop(promise: MithrilPromise) : MithrilPromiseProperty; + prop(value: T): MithrilProperty; + prop(): MithrilProperty; // might be that this should be Property + + withAttr(property: string, callback: (value: any) => void): (e: MithrilEvent) => any; + + module(rootElement: Node, module: MithrilModule): T; + module(rootElement: Node): T; + + trust(html: string): string; + + render(rootElement: Element|HTMLDocument): void; + render(rootElement: Element|HTMLDocument, children: MithrilVirtualElement, forceRecreation?: boolean): void; + render(rootElement: Element|HTMLDocument, children: MithrilVirtualElement[], forceRecreation?: boolean): void; + + redraw: { + (force?: boolean): void; + strategy: MithrilProperty; + } + + route: { + (rootElement: HTMLDocument, defaultRoute: string, routes: MithrilRoutes): void; + (rootElement: Element, defaultRoute: string, routes: MithrilRoutes): void; + + (element: Element, isInitialized: boolean): void; + (path: string, params?: any, shouldReplaceHistory?: boolean): void; + (): string; + + param(key: string): string; + mode: string; + } + + request(options: MithrilXHROptions): MithrilPromise; + + deferred: { + onerror(e: Error): void; + (): MithrilDeferred; + } + + sync(promises: MithrilPromise[]): MithrilPromise; + + startComputation(): void; + endComputation(): void; + + // For test suite + deps: { + (mockWindow: Window): Window; + factory: Object; + } + } interface MithrilVirtualElement { - tag: string; - attrs: Object; - children: any; + key?: number; + tag?: string; + attrs?: MithrilAttributes; + children?: any[]; } -interface MithrilModule { - controller: Function; - view: Function; +// Configuration function for an element +interface MithrilElementConfig { + (element: Element, isInitialized: boolean, context?: any): void; } -interface MithrilDeferred { - resolve(value?: any): void; - reject(value?: any): void; - promise: MithrilPromise; +// Attributes on a virtual element +interface MithrilAttributes { + title?: string; + className?: string; + class?: string; + config?: MithrilElementConfig; } -interface MithrilPromise { - (value?: any): any; - then(successCallback?: (value: any) => any, errorCallback?: (value: any) => any): MithrilPromise; +// Defines the subset of Event that Mithril needs +interface MithrilEvent { + currentTarget: Element; } +interface MithrilController { + (): any; + onunload?(evt: Event): any; +} + +interface MithrilView { + (ctrl: T): string|MithrilVirtualElement; +} + +interface MithrilModule { + controller: T; + view: MithrilView; +} + +interface MithrilProperty { + (): T; + (value: T): T; + toJSON(): T; +} + +interface MithrilPromiseProperty extends MithrilPromise { + (): T; + (value: T): T; + toJSON(): T; +} + +interface MithrilRoutes { + [key: string]: MithrilModule; +} + + +interface MithrilDeferred { + resolve(value?: T): void; + reject(value?: any): void; + promise: MithrilPromise; +} + +interface MithrilSuccessCallback { + (value: T): U; + (value: T): MithrilPromise; +} + +interface MithrilErrorCallback { + (value: Error): U; + (value: string): U; +} + +interface MithrilPromise { + (): T; + (value: T): T; + then(success: (value: T) => U): MithrilPromise; + then(success: (value: T) => MithrilPromise): MithrilPromise; + then(success: (value: T) => U, error: (value: Error) => V): MithrilPromise|MithrilPromise; + then(success: (value: T) => MithrilPromise, error: (value: Error) => V): MithrilPromise|MithrilPromise; +} interface MithrilXHROptions { - method: string; - url: string; - user?: string; - password?: string; - data?: any; - background?: boolean; - unwrapSuccess?(data: any): any; - unwrapError?(data: any): any; - serialize?(dataToSerialize: any): string; - deserialize?(dataToDeserialize: string): any; - extract?(xhr: XMLHttpRequest, options: MithrilXHROptions): string; - type?(data: Object): void; - config?(xhr: XMLHttpRequest, options: MithrilXHROptions): XMLHttpRequest; + method?: string; + url: string; + user?: string; + password?: string; + data?: any; + background?: boolean; + unwrapSuccess?(data: any): any; + unwrapError?(data: any): any; + serialize?(dataToSerialize: any): string; + deserialize?(dataToDeserialize: string): any; + extract?(xhr: XMLHttpRequest, options: MithrilXHROptions): string; + type?(data: Object): void; + config?(xhr: XMLHttpRequest, options: MithrilXHROptions): XMLHttpRequest; + dataType?: string; } declare var Mithril: MithrilStatic; declare var m: MithrilStatic; declare module 'mithril' { - export = MithrilStatic; + export = m; }