Stricter TypeScript definitions

This commit is contained in:
Chris Bowdon 2015-02-11 20:51:48 +00:00
parent 2369586167
commit 492eb2ed67

154
mithril.d.ts vendored
View file

@ -1,59 +1,144 @@
//Mithril type definitions for Typescript //Mithril type definitions for Typescript
interface MithrilStatic { interface MithrilStatic {
(selector: string, attributes: Object, children?: any): MithrilVirtualElement;
(selector: string, children?: any): MithrilVirtualElement; (selector: string, attributes: MithrilAttributes, ...children: Array<string|MithrilVirtualElement>): MithrilVirtualElement;
prop(value?: any): (value?: any) => any; (selector: string, ...children: Array<string|MithrilVirtualElement>): MithrilVirtualElement;
withAttr(property: string, callback: (value: any) => void): (e: Event) => any;
module(rootElement: Node, module?: MithrilModule): Object; prop<T>(promise: MithrilPromise<T>) : MithrilPromiseProperty<T>;
trust(html: string): String; prop<T>(value: T): MithrilProperty<T>;
render(rootElement: Element, children?: any): void; prop(): MithrilProperty<Object>; // might be that this should be Property<any>
render(rootElement: HTMLDocument, children?: any): void;
withAttr(property: string, callback: (value: any) => void): (e: MithrilEvent) => any;
module<T extends MithrilController>(rootElement: Node, module: MithrilModule<T>): T;
module<T extends MithrilController>(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: { redraw: {
(): void; (force?: boolean): void;
strategy(key: string); strategy: MithrilProperty<string>;
}; }
route: { route: {
(rootElement:Element, defaultRoute:string, routes:{ [key: string]: MithrilModule }): void <T extends MithrilController>(rootElement: HTMLDocument, defaultRoute: string, routes: MithrilRoutes<T>): void;
<T extends MithrilController>(rootElement: Element, defaultRoute: string, routes: MithrilRoutes<T>): void;
(element: Element, isInitialized: boolean): void; (element: Element, isInitialized: boolean): void;
(rootElement:HTMLDocument, defaultRoute:string, routes:{ [key: string]: MithrilModule }): void; (path: string, params?: any, shouldReplaceHistory?: boolean): void;
(path:string, params?:any, shouldReplaceHistory?:boolean): void;
(): string; (): string;
param(key:string): string;
param(key: string): string;
mode: string; mode: string;
}; }
request(options: MithrilXHROptions): MithrilPromise;
deferred(): MithrilDeferred; request<T>(options: MithrilXHROptions): MithrilPromise<T>;
sync(promises: MithrilPromise[]): MithrilPromise;
deferred: {
onerror(e: Error): void;
<T>(): MithrilDeferred<T>;
}
sync<T>(promises: MithrilPromise<T>[]): MithrilPromise<T[]>;
startComputation(): void; startComputation(): void;
endComputation(): void; endComputation(): void;
deps(Object: any): Object;
// For test suite
deps: {
(mockWindow: Window): Window;
factory: Object;
}
} }
interface MithrilVirtualElement { interface MithrilVirtualElement {
tag: string; key?: number;
attrs: Object; tag?: string;
children: any; attrs?: MithrilAttributes;
children?: any[];
} }
interface MithrilModule { // Configuration function for an element
controller: Function; interface MithrilElementConfig {
view: Function; (element: Element, isInitialized: boolean, context?: any): void;
} }
interface MithrilDeferred { // Attributes on a virtual element
resolve(value?: any): void; interface MithrilAttributes {
title?: string;
className?: string;
class?: string;
config?: MithrilElementConfig;
}
// Defines the subset of Event that Mithril needs
interface MithrilEvent {
currentTarget: Element;
}
interface MithrilController {
(): any;
onunload?(evt: Event): any;
}
interface MithrilView<T extends MithrilController> {
(ctrl: T): string|MithrilVirtualElement;
}
interface MithrilModule<T extends MithrilController> {
controller: T;
view: MithrilView<T>;
}
interface MithrilProperty<T> {
(): T;
(value: T): T;
toJSON(): T;
}
interface MithrilPromiseProperty<T> extends MithrilPromise<T> {
(): T;
(value: T): T;
toJSON(): T;
}
interface MithrilRoutes<T extends MithrilController> {
[key: string]: MithrilModule<T>;
}
interface MithrilDeferred<T> {
resolve(value?: T): void;
reject(value?: any): void; reject(value?: any): void;
promise: MithrilPromise; promise: MithrilPromise<T>;
} }
interface MithrilPromise { interface MithrilSuccessCallback<T, U> {
(value?: any): any; (value: T): U;
then(successCallback?: (value: any) => any, errorCallback?: (value: any) => any): MithrilPromise; (value: T): MithrilPromise<U>;
} }
interface MithrilErrorCallback<U> {
(value: Error): U;
(value: string): U;
}
interface MithrilPromise<T> {
(): T;
(value: T): T;
then<U>(success: (value: T) => U): MithrilPromise<U>;
then<U>(success: (value: T) => MithrilPromise<U>): MithrilPromise<U>;
then<U,V>(success: (value: T) => U, error: (value: Error) => V): MithrilPromise<U>|MithrilPromise<V>;
then<U,V>(success: (value: T) => MithrilPromise<U>, error: (value: Error) => V): MithrilPromise<U>|MithrilPromise<V>;
}
interface MithrilXHROptions { interface MithrilXHROptions {
method: string; method?: string;
url: string; url: string;
user?: string; user?: string;
password?: string; password?: string;
@ -66,11 +151,12 @@ interface MithrilXHROptions {
extract?(xhr: XMLHttpRequest, options: MithrilXHROptions): string; extract?(xhr: XMLHttpRequest, options: MithrilXHROptions): string;
type?(data: Object): void; type?(data: Object): void;
config?(xhr: XMLHttpRequest, options: MithrilXHROptions): XMLHttpRequest; config?(xhr: XMLHttpRequest, options: MithrilXHROptions): XMLHttpRequest;
dataType?: string;
} }
declare var Mithril: MithrilStatic; declare var Mithril: MithrilStatic;
declare var m: MithrilStatic; declare var m: MithrilStatic;
declare module 'mithril' { declare module 'mithril' {
export = MithrilStatic; export = m;
} }