From eaeb1cba08646bb26fc649067e0ad8588f6dca22 Mon Sep 17 00:00:00 2001 From: spacejack Date: Thu, 30 Mar 2017 21:48:47 -0400 Subject: [PATCH] Type signature fixes --- types/mithril/index.d.ts | 68 ++++++++++++++++----------------- types/mithril/stream/index.d.ts | 4 +- 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/types/mithril/index.d.ts b/types/mithril/index.d.ts index 069d162c76..655ed3a66c 100644 --- a/types/mithril/index.d.ts +++ b/types/mithril/index.d.ts @@ -1,13 +1,11 @@ // Type definitions for Mithril 1.1 -// Project: http://lhorie.github.io/mithril/ -// Definitions by: Leo Horie , Chris Bowdon , Mike Linkovich , András Parditka +// Project: https://mithril.js.org/ +// Definitions by: Mike Linkovich , András Parditka , Isiah Meadows // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 declare namespace Mithril { export interface Lifecycle { - /** Any property attached to the component object is copied for every instance of the component. This allows simple state initialization. */ - [propName: string]: any; /** The oninit hook is called before a vnode is touched by the virtual DOM engine. */ oninit?(this: State, vnode: Vnode): any; /** The oncreate hook is called after a DOM element is created and attached to the document. */ @@ -83,14 +81,12 @@ declare namespace Mithril { (element: Element, component: null): void; } - /** Creates an event handler which takes the value of the specified DOM element property and calls a function with it as the argument. */ - export type WithAttr = (name: string, callback: (value: any) => any, thisArg?: any) => (e: { currentTarget: any, [p: string]: any }) => void; - - /** Returns an object with key/value pairs parsed from a string of the form: ?a=1&b=2 */ - export type ParseQueryString = (queryString: string) => { [p: string]: any }; - - /** Turns the key/value pairs of an object into a string of the form: a=1&b=2 */ - export type BuildQueryString = (values: { [p: string]: any }) => string; + 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. */ @@ -108,7 +104,7 @@ declare namespace Mithril { /** Exposes the underlying XMLHttpRequest object for low-level configuration. */ config?(xhr: XMLHttpRequest): any; /** Headers to append to the request before sending it. */ - headers?: any; + headers?: { [key: string]: string }; /** A constructor to be applied to each object in the response. */ type?: new (o: any) => any; /** A serialization method to be applied to data. Defaults to JSON.stringify, or if options.data is an instance of FormData, defaults to the identity function. */ @@ -116,21 +112,16 @@ declare namespace Mithril { /** A deserialization method to be applied to the response. Defaults to a small wrapper around JSON.parse that returns null for empty responses. */ deserialize?(data: string): T; /** A hook to specify how the XMLHttpRequest response should be read. Useful for reading response headers and cookies. Defaults to a function that returns xhr.responseText */ - extract?(xhr: XMLHttpRequest, options: RequestOptions): T; + extract?(xhr: XMLHttpRequest, options: this): T; /** Force the use of the HTTP body section for data in GET requests when set to true, or the use of querystring for other HTTP methods when set to false. Defaults to false for GET requests and true for other methods. */ useBody?: boolean; /** If false, redraws mounted components upon completion of the request. If true, it does not. */ background?: boolean; } - export interface RequestOptionsAll extends RequestOptions { - /** The URL to send the request to. */ - url: string; - } - export interface Request { /** Makes an XHR request and returns a promise. */ - (options: RequestOptionsAll): Promise; + (options: RequestOptions & { url: string }): Promise; /** Makes an XHR request and returns a promise. */ (url: string, options?: RequestOptions): Promise; } @@ -148,14 +139,9 @@ declare namespace Mithril { background?: boolean; } - export interface JsonpOptionsAll extends JsonpOptions { - /** The URL to send the request to. */ - url: string; - } - export interface Jsonp { /** Makes a JSON-P request and returns a promise. */ - (options: JsonpOptionsAll): Promise; + (options: JsonpOptions & { url: string }): Promise; /** Makes a JSON-P request and returns a promise. */ (url: string, options?: JsonpOptions): Promise; } @@ -190,10 +176,10 @@ declare namespace Mithril { redraw: Redraw; request: Request; jsonp: Jsonp; - /** Parse a query string into an object. */ - parseQueryString: ParseQueryString; - /** Serialize an object into a query string. */ - buildQueryString: BuildQueryString; + /** 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 */ + buildQueryString(values: { [p: string]: any }): string; /** A string containing the semver value for the current Mithril release. */ version: string; } @@ -222,7 +208,7 @@ declare namespace Mithril { // In some lifecycle methods, Vnode will have a dom property // and possibly a domSize property. - export interface VnodeDOM extends Vnode { + export interface VnodeDOM> extends Vnode { /** Points to the element that corresponds to the vnode. */ dom: Element; /** This defines the number of DOM elements that the vnode represents (starting from the element referenced by the dom property). */ @@ -233,17 +219,31 @@ declare namespace Mithril { export interface CVnodeDOM extends VnodeDOM> { } - /** 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. */ + /** 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 can be used as a Mithril component. Components can be consumed via the m() utility. */ export interface Component> extends Lifecycle { /** Creates a view out of virtual elements. */ view(this: State, vnode: Vnode): Children | null | void; } + /** Components are a mechanism to encapsulate parts of a view to make code easier to organize and/or reuse. Any class that implements a view method can be used as a Mithril component. Components can be consumed via the m() utility. */ export interface ClassComponent extends Lifecycle> { - view(this: ClassComponent, vnode: CVnode): Children | null | void; + /** The oninit hook is called before a vnode is touched by the virtual DOM engine. */ + oninit?(vnode: Vnode): any; + /** The oncreate hook is called after a DOM element is created and attached to the document. */ + oncreate?(vnode: VnodeDOM): any; + /** The onbeforeupdate hook is called before a vnode is diffed in a update. */ + onbeforeremove?(vnode: VnodeDOM): Promise | void; + /** The onupdate hook is called after a DOM element is updated, while attached to the document. */ + onremove?(vnode: VnodeDOM): any; + /** 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. */ + onbeforeupdate?(vnode: Vnode, old: VnodeDOM): boolean | void; + /** The onremove hook is called before a DOM element is removed from the document. */ + onupdate?(vnode: VnodeDOM): any; + /** Creates a view out of virtual elements. */ + view(vnode: CVnode): Children | null | void; } - // Factory component + /** Components are a mechanism to encapsulate parts of a view to make code easier to organize and/or reuse. Any function that returns an object with a view method can be used as a Mithril component. Components can be consumed via the m() utility. */ export type FactoryComponent = (vnode: Vnode) => Component /** 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. */ diff --git a/types/mithril/stream/index.d.ts b/types/mithril/stream/index.d.ts index 7c5c1b3be0..4eb769e798 100644 --- a/types/mithril/stream/index.d.ts +++ b/types/mithril/stream/index.d.ts @@ -1,6 +1,4 @@ declare namespace Stream { - export type Combiner = (...streams: any[]) => T; - export interface Stream { /** Returns the value of the stream. */ (): T; @@ -26,7 +24,7 @@ declare namespace Stream { /** Creates a stream. */ (value?: T): Stream; /** Creates a computed stream that reactively updates if any of its upstreams are updated. */ - combine(combiner: Combiner, streams: Stream[]): Stream; + combine(combiner: (...streams: any[]) => T, streams: Stream[]): Stream; /** Creates a stream whose value is the array of values from an array of streams. */ merge(streams: Stream[]): Stream; /** Creates a new stream with the results of calling the function on every incoming stream with and accumulator and the incoming value. */