diff --git a/mithril/mithril.d.ts b/mithril/mithril.d.ts index 3cd0e21e49..c2304e7ece 100644 --- a/mithril/mithril.d.ts +++ b/mithril/mithril.d.ts @@ -5,90 +5,174 @@ //Mithril type definitions for Typescript -interface MithrilStatic { - (selector: string, attributes: Object, children?: any): MithrilVirtualElement; - (selector: string, children?: any): MithrilVirtualElement; - prop(value?: T): (value?: T) => T; - prop(promise: MithrilPromise): MithrilPromiseProperty; - withAttr(property: string, callback: (value: any) => void): (e: Event) => any; - module(rootElement: Node, module: MithrilModule): void; - trust(html: string): String; - render(rootElement: Element, children?: any): void; - render(rootElement: HTMLDocument, children?: any): void; - redraw: MithrilRedraw; - route: MithrilRoute; - request(options: MithrilXHROptions): MithrilPromise; - deferred(): MithrilDeferred; - sync(promises: MithrilPromise[]): MithrilPromise; - startComputation(): void; - endComputation(): void; +declare module _mithril { + interface MithrilStatic { + + (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, component: MithrilComponent): T; + module(rootElement: Node): T; + mount(rootElement: Node, component: MithrilComponent): T; + mount(rootElement: Node): T; + + component(component: MithrilComponent, ...args: Array): MithrilComponent + + 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, context: Object, vdom: Object): void; + (path: string, params?: any, shouldReplaceHistory?: boolean): void; + (): string; + + param(key: string): string; + mode: string; + buildQueryString(data: Object): String + parseQueryString(data: String): Object + } + + 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; + } + + } + + export interface MithrilVirtualElement { + key?: number; + tag?: string; + attrs?: MithrilAttributes; + children?: any[]; + } + + // Configuration function for an element + interface MithrilElementConfig { + (element: Element, isInitialized: boolean, context?: any, vdom?: MithrilVirtualElement): void; + } + + // Attributes on a virtual element + interface MithrilAttributes { + title?: string; + className?: string; + class?: string; + config?: MithrilElementConfig; + } + + // Defines the subset of Event that Mithril needs + interface MithrilEvent { + currentTarget: Element; + } + + interface MithrilController { + onunload?(evt: Event): any; + } + + interface MithrilControllerFunction extends MithrilController { + (): any; + } + + interface MithrilView { + (ctrl: T): string|MithrilVirtualElement; + } + + interface MithrilComponent { + controller: MithrilControllerFunction|{ new(): 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]: MithrilComponent; + } + + + 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; + dataType?: string; + } } -interface MithrilRoute { - (rootElement: Element, defaultRoute: string, routes: { [key: string]: MithrilModule }): void; - (rootElement: HTMLDocument, defaultRoute: string, routes: { [key: string]: MithrilModule }): void; - (path: string, params?: any, shouldReplaceHistory?: boolean): void; - (element: Element, isInitialized: boolean): void; - (): string; - mode: string; - param: MithrilParam; - buildQueryString(data: Object): string; - parseQueryString(queryString: string): Object; -} +declare var Mithril: _mithril.MithrilStatic; +declare var m: _mithril.MithrilStatic; -interface MithrilParam { - (param: string): string; +declare module "mithril" { + export = m; } - -interface MithrilRedraw { - (): void; - strategy: (value?: string) => string; -} - -interface MithrilVirtualElement { - tag: string; - attrs: Object; - children: any; -} - -interface MithrilModule { - controller: Function; - view: (controller?: any) => MithrilVirtualElement; -} - -interface MithrilDeferred { - resolve(value?: T): void; - reject(value?: any): void; - promise: MithrilPromise; -} - -interface MithrilPromise { - (value?: T): T; - then(successCallback?: (value: T) => R, errorCallback?: (value: any) => any): MithrilPromise; - then(successCallback?: (value: T) => MithrilPromise, errorCallback?: (value: any) => any): MithrilPromise; -} - -interface MithrilPromiseProperty extends MithrilPromise { - (): T; - (value: T): T; - toJSON(): T; -} - -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; -} - -declare var Mithril: MithrilStatic; -declare var m: MithrilStatic;