// Type definitions for mithril.js 1.0 // Project: https://github.com/lhorie/mithril.js // Definitions by: Mike Linkovich declare namespace Mithril { interface Lifecycle { /** The oninit hook is called before a vnode is touched by the virtual DOM engine. */ oninit?: (this: S, vnode: Vnode) => void; /** The oncreate hook is called after a DOM element is created and attached to the document. */ oncreate?: (this: S, vnode: VnodeDOM) => void; /** The onbeforeupdate hook is called before a vnode is diffed in a update. */ onbeforeupdate?: (this: S, vnode: Vnode, old: Vnode) => boolean; /** The onupdate hook is called after a DOM element is updated, while attached to the document. */ onupdate?: (this: S, vnode: VnodeDOM) => void; /** The onbeforeremove hook is called before a DOM element is detached from the document. If a Promise is returned, Mithril only detaches the DOM element after the promise completes. */ onbeforeremove?: (this: S, vnode: VnodeDOM) => Promise | void; /** The onremove hook is called before a DOM element is removed from the document. */ onremove?: (this: S, vnode: VnodeDOM) => void; } interface Hyperscript { /** Creates a virtual element (Vnode). */ (selector: string, ...children: any[]): Vnode; /** Creates a virtual element (Vnode). */ (component: Component | {new(vnode: CVnode): ClassComponent} | FactoryComponent, a?: (A & Lifecycle) | Children, ...children: Children[]): Vnode; /** Creates a fragment virtual element (Vnode). */ fragment(attrs: any, children: Children[]): Vnode; /** Turns an HTML string into a virtual element (Vnode). Do not use trust on unsanitized user input. */ trust(html: string): Vnode; } interface RouteResolver { /** The onmatch hook is called when the router needs to find a component to render. */ onmatch?: (args: any, requestedPath: string) => Mithril.Component | {new(vnode: CVnode): ClassComponent} | FactoryComponent | Promise | {new(): Component} | FactoryComponent> | void; /** The render method is called on every redraw for a matching route. */ render?: (vnode: Mithril.Vnode) => Children; } interface RouteDefs { [url: string]: Component | {new(vnode: CVnode): ClassComponent} | FactoryComponent | RouteResolver; } interface RouteOptions { replace?: boolean; state?: any; title?: string; } interface Route { /** Creates application routes and mounts Components and/or RouteResolvers to a DOM element. */ (element: HTMLElement, defaultRoute: string, routes: RouteDefs): void; /** Returns the last fully resolved routing path, without the prefix. */ get(): string; /** Redirects to a matching route or to the default route if no matching routes can be found. */ set(route: string, data?: any, options?: RouteOptions): void; /** Defines a router prefix which is a fragment of the URL that dictates the underlying strategy used by the router. */ prefix(urlFragment: string): void; /** This method is meant to be used in conjunction with an Vnode's oncreate hook. */ link(vnode: Vnode): (e: Event) => void; /** Returns the named parameter value from the current route. */ param(name?: string): any; } interface Mount { /** Mounts a component to a DOM element, enabling it to autoredraw on user events. */ (element: Element, component: Component | {new(vnode: CVnode): ClassComponent} | FactoryComponent | null): void; } interface WithAttr { /** Creates an event handler which takes the value of the specified DOM element property and calls a function with it as the argument. */ (name: string, callback: (value: any) => void, thisArg?: any): (e: {currentTarget: any, [p: string]: any}) => boolean; } interface ParseQueryString { /** Returns an object with key/value pairs parsed from a string of the form: ?a=1&b=2 */ (queryString: string): any; } interface BuildQueryString { /** Turns the key/value pairs of an object into a string of the form: a=1&b=2 */ (values: {[p: string]: any}): string; } interface RequestOptions { method?: string; data?: any; async?: boolean; user?: string; password?: string; withCredentials?: boolean; config?: (xhr: XMLHttpRequest) => void; headers?: any; type?: any; serialize?: (data: any) => string; deserialize?: (str: string) => T; extract?: (xhr: XMLHttpRequest, options: RequestOptions) => string; useBody?: boolean; background?: boolean; } interface RequestOptionsAll extends RequestOptions { url: string; } interface Request { /** Makes an XHR request and returns a promise. */ (options: RequestOptionsAll): Promise; /** Makes an XHR request and returns a promise. */ (url: string, options?: RequestOptions): Promise; } interface JsonpOptions { data?: any; type?: any; callbackName?: string; callbackKey?: string; background?: boolean; } interface JsonpOptionsAll extends JsonpOptions { url: string; } interface Jsonp { /** Makes a JSON-P request and returns a promise. */ (options: JsonpOptionsAll): Promise; /** Makes a JSON-P request and returns a promise. */ (url: string, options?: JsonpOptions): Promise; } interface RequestService { request: Request; jsonp: Jsonp; } interface Render { /** Renders a vnode structure into a DOM element. */ (el: Element, vnodes: Children): void; } interface RenderService { render: Render } interface Redraw { /** Manually triggers a redraw of mounted components. */ (): void; } interface RedrawService { redraw: Redraw render: Render } interface Static extends Hyperscript { route: Route; mount: Mount; withAttr: WithAttr; render: Render; redraw: Redraw; request: Request; jsonp: Jsonp; parseQueryString: ParseQueryString; buildQueryString: BuildQueryString; version: string; } // Vnode children types type Child = Vnode | string | number | boolean | null | undefined; interface ChildArray extends Array {} type Children = Child | ChildArray; interface Vnode> { tag: string | Component; attrs: A; state: S; key?: string; children?: Vnode[]; events?: any; } // In some lifecycle methods, Vnode will have a dom property // and possibly a domSize property. interface VnodeDOM extends Vnode { dom: Element; domSize?: number; } interface CVnode extends Vnode> {} interface CVnodeDOM extends VnodeDOM> {} interface Component> extends Lifecycle { view (this: S, vnode: Vnode): Vnode | null | void | (Vnode | null | void)[]; } interface ClassComponent extends Lifecycle> { view (this: ClassComponent, vnode: CVnode): Vnode | null | void | (Vnode | null | void)[]; } // Factory component type FactoryComponent = (vnode: Vnode) => Component type Unary = (input: T) => U; interface Functor { map(f: Unary): Functor; ap?(f: Functor): Functor; } interface Stream { /** Returns the value of the stream. */ (): T; /** Sets the value of the stream. */ (value: T): this; /** Creates a dependent stream whose value is set to the result of the callback function. */ map(f: (current: T) => Stream | T | void): Stream; /** Creates a dependent stream whose value is set to the result of the callback function. */ map(f: (current: T) => Stream | U): Stream; /** This method is functionally identical to stream. It exists to conform to Fantasy Land's Applicative specification. */ of(val?: T): Stream; /** Apply. */ ap(f: Stream<(value: T) => U>): Stream; /** A co-dependent stream that unregisters dependent streams when set to true. */ end: Stream; } type StreamCombiner = (...streams: any[]) => T interface StreamFactory { /** Creates a stream. */ (val?: T): Stream; /** Creates a computed stream that reactively updates if any of its upstreams are updated. */ combine(combiner: StreamCombiner, streams: Stream[]): Stream; /** Creates a stream whose value is the array of values from an array of streams. */ merge(streams: Stream[]): Stream; /** A special value that can be returned to stream callbacks to halt execution of downstreams. */ HALT: any; } } declare module 'mithril' { const m: Mithril.Static; export = m; } declare module 'mithril/hyperscript' { const h: Mithril.Hyperscript; export = h; } declare module 'mithril/mount' { const m: Mithril.Mount; export = m; } declare module 'mithril/route' { const r: Mithril.Route; export = r; } declare module 'mithril/request' { const r: Mithril.RequestService; export = r; } declare module 'mithril/render' { const r: Mithril.RenderService; export = r; } declare module 'mithril/redraw' { const r: Mithril.RedrawService; export = r; } declare module 'mithril/util/withAttr' { const withAttr: Mithril.WithAttr; export = withAttr; } declare module 'mithril/stream' { const s: Mithril.StreamFactory; export = s; }