diff --git a/types/mithril/index.d.ts b/types/mithril/index.d.ts index 9e5a4897e5..88b3886a18 100644 --- a/types/mithril/index.d.ts +++ b/types/mithril/index.d.ts @@ -4,6 +4,32 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 +/** Manually triggers a redraw of mounted components. */ +declare function redraw(): void; + +/** Renders a vnode structure into a DOM element. */ +declare function render(el: Element, vnodes: Mithril.Children): void; + +/** Mounts a component to a DOM element, enabling it to autoredraw on user events. */ +declare function mount(element: Element, component: Mithril.ComponentTypes): void; +/** Unmounts a component from a DOM element. */ +declare function mount(element: Element, component: null): void; + +/** Makes an XHR request and returns a promise. */ +declare function request (options: Mithril.RequestOptions & { url: string }): Promise; +/** Makes an XHR request and returns a promise. */ +declare function request (url: string, options?: Mithril.RequestOptions): Promise; + +/** Makes a JSON-P request and returns a promise. */ +declare function jsonp(options: Mithril.JsonpOptions & { url: string }): Promise; +/** Makes a JSON-P request and returns a promise. */ +declare function jsonp(url: string, options?: Mithril.JsonpOptions): Promise; + +/** Creates an event handler which takes the value of the specified DOM element property and calls a function with it as the argument. */ +declare function withAttr(name: string, callback: (value: any) => any): (e: { currentTarget: any, [p: string]: any }) => void; +/** Creates an event handler which takes the value of the specified DOM element property and calls a function with it as the argument. */ +declare function withAttr(name: string, callback: (this: T, value: any) => any, thisArg: T): (e: { currentTarget: any, [p: string]: any }) => void; + declare namespace Mithril { export interface Lifecycle { /** The oninit hook is called before a vnode is touched by the virtual DOM engine. */ @@ -74,20 +100,6 @@ declare namespace Mithril { param(): any; } - export interface Mount { - /** Mounts a component to a DOM element, enabling it to autoredraw on user events. */ - (element: Element, component: ComponentTypes): void; - /** Unmounts a component from a DOM element. */ - (element: Element, component: null): void; - } - - export 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) => any): (e: { currentTarget: any, [p: string]: any }) => void; - /** 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: (this: T, value: any) => any, thisArg: T): (e: { currentTarget: any, [p: string]: any }) => void; - } - export interface RequestOptions { /** The HTTP method to use. */ method?: string; @@ -119,13 +131,6 @@ declare namespace Mithril { background?: boolean; } - export interface Request { - /** Makes an XHR request and returns a promise. */ - (options: RequestOptions & { url: string }): Promise; - /** Makes an XHR request and returns a promise. */ - (url: string, options?: RequestOptions): Promise; - } - export interface JsonpOptions { /** The data to be interpolated into the URL and serialized into the querystring. */ data?: any; @@ -139,43 +144,14 @@ declare namespace Mithril { background?: boolean; } - export interface Jsonp { - /** Makes a JSON-P request and returns a promise. */ - (options: JsonpOptions & { url: string }): Promise; - /** Makes a JSON-P request and returns a promise. */ - (url: string, options?: JsonpOptions): Promise; - } - - export interface RequestService { - request: Request; - jsonp: Jsonp; - } - - /** Renders a vnode structure into a DOM element. */ - export type Render = (el: Element, vnodes: Children) => void; - - export interface RenderService { - render: Render - } - - /** Manually triggers a redraw of mounted components. */ - export type Redraw = () => void; - - export interface RedrawService { - redraw: Redraw - render: Render - } - export interface Static extends Hyperscript { route: Route; - /** Activates a component, enabling it to autoredraw on user events. */ - mount: Mount; - /** Returns a event handler that can be bound to an element, firing with the specified property. */ - withAttr: WithAttr; - render: Render; - redraw: Redraw; - request: Request; - jsonp: Jsonp; + mount: typeof mount; + withAttr: typeof withAttr; + render: typeof render; + redraw: typeof redraw; + request: typeof request; + jsonp: typeof jsonp; /** Returns an object with key/value pairs parsed from a string of the form: ?a=1&b=2 */ parseQueryString(queryString: string): { [p: string]: any }; /** Turns the key/value pairs of an object into a string of the form: a=1&b=2 */ @@ -249,6 +225,7 @@ declare namespace Mithril { /** Components are a mechanism to encapsulate parts of a view to make code easier to organize and/or reuse. Any Javascript object that has a view method is a Mithril component. Components can be consumed via the m() utility. */ export type Comp> = Component & State; + /** Components are a mechanism to encapsulate parts of a view to make code easier to organize and/or reuse. Components can be consumed via the m() utility. */ export type ComponentTypes = Component | { new (vnode: CVnode): ClassComponent } | FactoryComponent /** This represents the attributes available for configuring virtual elements, beyond the applicable DOM attributes. */ diff --git a/types/mithril/mount.d.ts b/types/mithril/mount.d.ts index c177527f84..af79b918a9 100644 --- a/types/mithril/mount.d.ts +++ b/types/mithril/mount.d.ts @@ -1,3 +1,3 @@ -import { Mount } from "mithril"; -declare const mount: Mount; +import { mount as _mount } from "mithril"; +declare const mount: typeof _mount; export = mount; diff --git a/types/mithril/redraw.d.ts b/types/mithril/redraw.d.ts index 8206db5c0e..26c0a36b03 100644 --- a/types/mithril/redraw.d.ts +++ b/types/mithril/redraw.d.ts @@ -1,3 +1,11 @@ -import { Redraw } from "mithril"; -declare const redraw: Redraw; -export = redraw; +import { redraw, render } from "mithril"; + +declare namespace RedrawService { + export interface Static { + render: typeof render; + redraw: typeof redraw; + } +} + +declare const RedrawService: RedrawService.Static; +export = RedrawService; diff --git a/types/mithril/render.d.ts b/types/mithril/render.d.ts index 72651f42b4..d1144ced0d 100644 --- a/types/mithril/render.d.ts +++ b/types/mithril/render.d.ts @@ -1,3 +1,10 @@ -import { Render } from "mithril"; -declare const render: Render; -export = render; +import { render } from "mithril"; + +declare namespace RenderService { + export interface Static { + render: typeof render; + } +} + +declare const RenderService: RenderService.Static; +export = RenderService; diff --git a/types/mithril/request.d.ts b/types/mithril/request.d.ts index 0e737a04e9..d4483c76d2 100644 --- a/types/mithril/request.d.ts +++ b/types/mithril/request.d.ts @@ -1,3 +1,11 @@ -import { Request } from "mithril"; -declare const request: Request; -export = request; +import { request, jsonp } from "mithril"; + +declare namespace RequestService { + export interface Static { + request: typeof request; + jsonp: typeof jsonp; + } +} + +declare const RequestService: RequestService.Static; +export = RequestService; diff --git a/types/mithril/test/test-jsonp.ts b/types/mithril/test/test-jsonp.ts index 32f40ca4b6..856fe8236d 100644 --- a/types/mithril/test/test-jsonp.ts +++ b/types/mithril/test/test-jsonp.ts @@ -1,10 +1,10 @@ -import * as m from 'mithril' +import {jsonp} from 'mithril/request' interface Result { id: number } -m.jsonp('/item').then(data => { +jsonp('/item').then(data => { console.log(data.id) }) @@ -15,7 +15,7 @@ class User { } } -m.jsonp({ +jsonp({ url: '/user', data: {test: 'abc'}, type: User, diff --git a/types/mithril/test/test-misc.ts b/types/mithril/test/test-misc.ts index 75f1367de8..97ce9b5344 100644 --- a/types/mithril/test/test-misc.ts +++ b/types/mithril/test/test-misc.ts @@ -1,16 +1,23 @@ -import * as m from 'mithril' +import {trust, parseQueryString, buildQueryString} from 'mithril' +import * as h from 'mithril/hyperscript' +import {render} from 'mithril/render' +import {redraw} from 'mithril/redraw' +import * as withAttr from 'mithril/withAttr' -const vnode = m.trust('Some bold text.') +const vnode = trust('Some bold text.') -const params = m.parseQueryString('?id=123') +const params = parseQueryString('?id=123') -const qstr = m.buildQueryString({id: 123}) +const qstr = buildQueryString({id: 123}) -m.render(document.body, 'Hello') -m.render(document.body, m('h1', 'Test')) -m.render(document.body, [ - m('h1', 'Test'), "abc", null, 123, false, m('p', 'Vnode array'), - ['a', 123, undefined, m('div', 'Nested')] +render(document.body, 'Hello') +render(document.body, h('h1', 'Test')) +render(document.body, [ + h('h1', 'Test'), "abc", null, 123, false, h('p', 'Vnode array'), + ['a', 123, undefined, h('div', 'Nested')] ]) -m.redraw() +redraw() + +const handler = withAttr("value", (value) => {}) +handler({currentTarget: {value: 10}}) diff --git a/types/mithril/test/test-request.ts b/types/mithril/test/test-request.ts index 8ea24fe091..7042790427 100644 --- a/types/mithril/test/test-request.ts +++ b/types/mithril/test/test-request.ts @@ -1,4 +1,4 @@ -import * as request from 'mithril/request' +import {request} from 'mithril/request' interface Result { id: number diff --git a/types/mithril/withAttr.d.ts b/types/mithril/withAttr.d.ts index 14623971a6..7d1ea786ad 100644 --- a/types/mithril/withAttr.d.ts +++ b/types/mithril/withAttr.d.ts @@ -1,3 +1,3 @@ -import { WithAttr } from "mithril"; -declare const withAttr: WithAttr; +import { withAttr as _withAttr } from "mithril"; +declare const withAttr: typeof _withAttr; export = withAttr;