From 1e879963f1c370de66aab80deb291826c8d999d3 Mon Sep 17 00:00:00 2001 From: spacejack Date: Tue, 14 Feb 2017 20:26:15 -0500 Subject: [PATCH] Add Typescript definitions --- mithril.d.ts | 237 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 2 files changed, 238 insertions(+) create mode 100644 mithril.d.ts diff --git a/mithril.d.ts b/mithril.d.ts new file mode 100644 index 00000000..b5a74cf2 --- /dev/null +++ b/mithril.d.ts @@ -0,0 +1,237 @@ +// Type definitions for mithril.js 1.0 +// Project: https://github.com/lhorie/mithril.js +// Definitions by: Mike Linkovich + +declare namespace Mithril { + + interface Lifecycle { + oninit?: (this: S, vnode: Vnode) => void; + oncreate?: (this: S, vnode: VnodeDOM) => void; + onbeforeremove?: (this: S, vnode: VnodeDOM) => Promise | void; + onremove?: (this: S, vnode: VnodeDOM) => void; + onbeforeupdate?: (this: S, vnode: Vnode, old: Vnode) => boolean; + onupdate?: (this: S, vnode: VnodeDOM) => void; + } + + interface Hyperscript { + (selector: string, ...children: any[]): Vnode; + (component: Component, a?: (A & Lifecycle) | Children, ...children: Children[]): Vnode; + fragment(attrs: any, children: Children[]): Vnode; + trust(html: string): Vnode; + } + + interface RouteResolver { + render?: (vnode: Mithril.Vnode) => Children; + onmatch?: (args: any, requestedPath: string) => Mithril.Component | Promise> | void; + } + + interface RouteDefs { + [url: string]: Component | RouteResolver; + } + + interface RouteOptions { + replace?: boolean; + state?: any; + title?: string; + } + + interface Route { + (element: HTMLElement, defaultRoute: string, routes: RouteDefs): void; + get(): string; + set(route: string, data?: any, options?: RouteOptions): void; + prefix(urlFragment: string): void; + link(vnode: Vnode): (e: Event) => void; + param(name?: string): any; + } + + interface Mount { + (element: Element, component: Component | null): void; + } + + interface WithAttr { + (name: string, stream: Stream, thisArg?: any): (e: {currentTarget: any, [p: string]: any}) => boolean; + (name: string, callback: (value: any) => void, thisArg?: any): (e: {currentTarget: any, [p: string]: any}) => boolean; + } + + interface ParseQueryString { + (queryString: string): any; + } + + interface BuildQueryString { + (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 { + (options: RequestOptionsAll): 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 { + (options: JsonpOptionsAll): Promise; + (url: string, options?: JsonpOptions): Promise; + } + + interface RequestService { + request: Request; + jsonp: Jsonp; + } + + interface Render { + (el: Element, vnodes: Children): void; + } + + interface RenderService { + render: Render + } + + interface Redraw { + (): 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 Component> extends Lifecycle { + view: (this: S, vnode: Vnode) => Vnode | null | void | (Vnode | null | void)[]; + } + + type Unary = (input: T) => U; + + interface Functor { + map(f: Unary): Functor; + ap?(f: Functor): Functor; + } + + interface Stream { + (): T; + (value: T): this; + map(f: (current: T) => Stream | T | void): Stream; + map(f: (current: T) => Stream | U): Stream; + of(val?: T): Stream; + ap(f: Stream<(value: T) => U>): Stream; + end: Stream; + } + + type StreamCombiner = (...streams: any[]) => T + + interface StreamFactory { + (val?: T): Stream; + combine(combiner: StreamCombiner, streams: Stream[]): Stream; + merge(streams: Stream[]): Stream; + 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; +} diff --git a/package.json b/package.json index 1417f13f..026dfe20 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "author": "Leo Horie", "license": "MIT", "main": "mithril.js", + "types": "mithril.d.ts", "repository": "lhorie/mithril.js", "scripts": { "dev": "node bundler/cli browser.js -o mithril.js -w",