From 8ce7e373034cd6c8315c46780e4bccd053d256d6 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Tue, 8 Nov 2016 10:39:20 -0800 Subject: [PATCH] Update react.d.ts used during tests --- tests/lib/react.d.ts | 2040 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 1755 insertions(+), 285 deletions(-) diff --git a/tests/lib/react.d.ts b/tests/lib/react.d.ts index 0a3b0fdfabf..b2b0783cf15 100644 --- a/tests/lib/react.d.ts +++ b/tests/lib/react.d.ts @@ -4,35 +4,51 @@ // Definitions: https://github.com/borisyankov/DefinitelyTyped declare namespace __React { + // // React Elements // ---------------------------------------------------------------------- type ReactType = string | ComponentClass | StatelessComponent; - interface ReactElement

> { - type: string | ComponentClass

| StatelessComponent

; + type Key = string | number; + type Ref = string | ((instance: T) => any); + type ComponentState = {} | void; + + interface Attributes { + key?: Key; + } + interface ClassAttributes extends Attributes { + ref?: Ref; + } + + interface ReactElement

{ + type: string | ComponentClass

| SFC

; props: P; - key: string | number; - ref: string | ((component: Component | Element) => any); + key?: Key; } - interface ClassicElement

extends ReactElement

{ - type: ClassicComponentClass

; - ref: string | ((component: ClassicComponent) => any); + interface SFCElement

extends ReactElement

{ + type: SFC

; } - interface DOMElement

> extends ReactElement

{ + type CElement> = ComponentElement; + interface ComponentElement> extends ReactElement

{ + type: ComponentClass

; + ref?: Ref; + } + + type ClassicElement

= CElement>; + + interface DOMElement

, T extends Element> extends ReactElement

{ type: string; - ref: string | ((element: Element) => any); + ref: Ref; } - interface ReactHTMLElement extends DOMElement> { - ref: string | ((element: HTMLElement) => any); + interface ReactHTMLElement extends DOMElement, T> { } - interface ReactSVGElement extends DOMElement { - ref: string | ((element: SVGElement) => any); + interface ReactSVGElement extends DOMElement, SVGElement> { } // @@ -40,19 +56,29 @@ declare namespace __React { // ---------------------------------------------------------------------- interface Factory

{ - (props?: P, ...children: ReactNode[]): ReactElement

; + (props?: P & Attributes, ...children: ReactNode[]): ReactElement

; } - interface ClassicFactory

extends Factory

{ - (props?: P, ...children: ReactNode[]): ClassicElement

; + interface SFCFactory

{ + (props?: P & Attributes, ...children: ReactNode[]): SFCElement

; } - interface DOMFactory

> extends Factory

{ - (props?: P, ...children: ReactNode[]): DOMElement

; + interface ComponentFactory> { + (props?: P & ClassAttributes, ...children: ReactNode[]): CElement; } - type HTMLFactory = DOMFactory>; - type SVGFactory = DOMFactory; + type CFactory> = ComponentFactory; + type ClassicFactory

= CFactory>; + + interface DOMFactory

, T extends Element> { + (props?: P & ClassAttributes, ...children: ReactNode[]): DOMElement; + } + + interface HTMLFactory extends DOMFactory, T> { + } + + interface SVGFactory extends DOMFactory, SVGElement> { + } // // React Nodes @@ -72,41 +98,59 @@ declare namespace __React { function createClass(spec: ComponentSpec): ClassicComponentClass

; - function createFactory

(type: string): DOMFactory

; - function createFactory

(type: ClassicComponentClass

): ClassicFactory

; - function createFactory

(type: ComponentClass

| StatelessComponent

): Factory

; + function createFactory

, T extends Element>( + type: string): DOMFactory; + function createFactory

(type: SFC

): SFCFactory

; + function createFactory

( + type: ClassType, ClassicComponentClass

>): CFactory>; + function createFactory, C extends ComponentClass

>( + type: ClassType): CFactory; + function createFactory

(type: ComponentClass

| SFC

): Factory

; - function createElement

( + function createElement

, T extends Element>( type: string, - props?: P, - ...children: ReactNode[]): DOMElement

; + props?: P & ClassAttributes, + ...children: ReactNode[]): DOMElement; function createElement

( - type: ClassicComponentClass

, - props?: P, - ...children: ReactNode[]): ClassicElement

; + type: SFC

, + props?: P & Attributes, + ...children: ReactNode[]): SFCElement

; function createElement

( - type: ComponentClass

| StatelessComponent

, - props?: P, + type: ClassType, ClassicComponentClass

>, + props?: P & ClassAttributes>, + ...children: ReactNode[]): CElement>; + function createElement, C extends ComponentClass

>( + type: ClassType, + props?: P & ClassAttributes, + ...children: ReactNode[]): CElement; + function createElement

( + type: ComponentClass

| SFC

, + props?: P & Attributes, ...children: ReactNode[]): ReactElement

; - function cloneElement

( - element: DOMElement

, - props?: P, - ...children: ReactNode[]): DOMElement

; - function cloneElement

( - element: ClassicElement

, - props?: P, - ...children: ReactNode[]): ClassicElement

; - function cloneElement

( + function cloneElement

, T extends Element>( + element: DOMElement, + props?: P & ClassAttributes, + ...children: ReactNode[]): DOMElement; + function cloneElement

( + element: SFCElement

, + props?: Q, // should be Q & Attributes, but then Q is inferred as {} + ...children: ReactNode[]): SFCElement

; + function cloneElement

>( + element: CElement, + props?: Q, // should be Q & ClassAttributes + ...children: ReactNode[]): CElement; + function cloneElement

( element: ReactElement

, - props?: P, + props?: Q, // should be Q & Attributes ...children: ReactNode[]): ReactElement

; - function isValidElement(object: {}): boolean; + function isValidElement

(object: {}): object is ReactElement

; var DOM: ReactDOM; var PropTypes: ReactPropTypes; var Children: ReactChildren; + var version: string; // // Component API @@ -120,8 +164,14 @@ declare namespace __React { setState(f: (prevState: S, props: P) => S, callback?: () => any): void; setState(state: S, callback?: () => any): void; forceUpdate(callBack?: () => any): void; - render(): JSX.Element; - props: P; + render(): JSX.Element | null; + + // React.Props is now deprecated, which means that the `children` + // property is not available on `P` by default, even though you can + // always pass children as variadic arguments to `createElement`. + // In the future, if we can define its call signature conditionally + // on the existence of `children` in `P`, then we should remove this. + props: P & { children?: ReactNode }; state: S; context: {}; refs: { @@ -129,6 +179,8 @@ declare namespace __React { }; } + class PureComponent extends Component {} + interface ClassicComponent extends Component { replaceState(nextState: S, callback?: () => any): void; isMounted(): boolean; @@ -143,27 +195,39 @@ declare namespace __React { // Class Interfaces // ---------------------------------------------------------------------- + type SFC

= StatelessComponent

; interface StatelessComponent

{ - (props?: P, context?: any): ReactElement; + (props: P, context?: any): ReactElement; propTypes?: ValidationMap

; contextTypes?: ValidationMap; defaultProps?: P; + displayName?: string; } interface ComponentClass

{ - new(props?: P, context?: any): Component; + new(props?: P, context?: any): Component; propTypes?: ValidationMap

; contextTypes?: ValidationMap; childContextTypes?: ValidationMap; defaultProps?: P; + displayName?: string; } interface ClassicComponentClass

extends ComponentClass

{ - new(props?: P, context?: any): ClassicComponent; + new(props?: P, context?: any): ClassicComponent; getDefaultProps?(): P; - displayName?: string; } + /** + * We use an intersection type to infer multiple type parameters from + * a single argument, which is useful for many top-level API defs. + * See https://github.com/Microsoft/TypeScript/issues/7234 for more info. + */ + type ClassType, C extends ComponentClass

> = + C & + (new() => T) & + (new() => { props: P }); + // // Component Specs and Lifecycle // ---------------------------------------------------------------------- @@ -187,7 +251,7 @@ declare namespace __React { displayName?: string; propTypes?: ValidationMap; contextTypes?: ValidationMap; - childContextTypes?: ValidationMap + childContextTypes?: ValidationMap; getDefaultProps?(): P; getInitialState?(): S; @@ -203,41 +267,44 @@ declare namespace __React { // Event System // ---------------------------------------------------------------------- - interface SyntheticEvent { + interface SyntheticEvent { bubbles: boolean; + currentTarget: EventTarget & T; cancelable: boolean; - currentTarget: EventTarget; defaultPrevented: boolean; eventPhase: number; isTrusted: boolean; nativeEvent: Event; preventDefault(): void; + isDefaultPrevented(): boolean; stopPropagation(): void; + isPropagationStopped(): boolean; + persist(): void; target: EventTarget; timeStamp: Date; type: string; } - interface ClipboardEvent extends SyntheticEvent { + interface ClipboardEvent extends SyntheticEvent { clipboardData: DataTransfer; } - interface CompositionEvent extends SyntheticEvent { + interface CompositionEvent extends SyntheticEvent { data: string; } - interface DragEvent extends SyntheticEvent { + interface DragEvent extends MouseEvent { dataTransfer: DataTransfer; } - interface FocusEvent extends SyntheticEvent { + interface FocusEvent extends SyntheticEvent { relatedTarget: EventTarget; } - interface FormEvent extends SyntheticEvent { + interface FormEvent extends SyntheticEvent { } - interface KeyboardEvent extends SyntheticEvent { + interface KeyboardEvent extends SyntheticEvent { altKey: boolean; charCode: number; ctrlKey: boolean; @@ -252,7 +319,7 @@ declare namespace __React { which: number; } - interface MouseEvent extends SyntheticEvent { + interface MouseEvent extends SyntheticEvent { altKey: boolean; button: number; buttons: number; @@ -269,7 +336,7 @@ declare namespace __React { shiftKey: boolean; } - interface TouchEvent extends SyntheticEvent { + interface TouchEvent extends SyntheticEvent { altKey: boolean; changedTouches: TouchList; ctrlKey: boolean; @@ -280,178 +347,1577 @@ declare namespace __React { touches: TouchList; } - interface UIEvent extends SyntheticEvent { + interface UIEvent extends SyntheticEvent { detail: number; view: AbstractView; } - interface WheelEvent extends SyntheticEvent { + interface WheelEvent extends MouseEvent { deltaMode: number; deltaX: number; deltaY: number; deltaZ: number; } + interface AnimationEvent extends SyntheticEvent<{}> { + animationName: string; + pseudoElement: string; + elapsedTime: number; + } + + interface TransitionEvent extends SyntheticEvent<{}> { + propertyName: string; + pseudoElement: string; + elapsedTime: number; + } + // // Event Handler Types // ---------------------------------------------------------------------- - interface EventHandler { + interface EventHandler> { (event: E): void; } - type ReactEventHandler = EventHandler; + type ReactEventHandler = EventHandler>; - type ClipboardEventHandler = EventHandler; - type CompositionEventHandler = EventHandler; - type DragEventHandler = EventHandler; - type FocusEventHandler = EventHandler; - type FormEventHandler = EventHandler; - type KeyboardEventHandler = EventHandler; - type MouseEventHandler = EventHandler; - type TouchEventHandler = EventHandler; - type UIEventHandler = EventHandler; - type WheelEventHandler = EventHandler; + type ClipboardEventHandler = EventHandler>; + type CompositionEventHandler = EventHandler>; + type DragEventHandler = EventHandler>; + type FocusEventHandler = EventHandler>; + type FormEventHandler = EventHandler>; + type KeyboardEventHandler = EventHandler>; + type MouseEventHandler = EventHandler>; + type TouchEventHandler = EventHandler>; + type UIEventHandler = EventHandler>; + type WheelEventHandler = EventHandler>; + type AnimationEventHandler = EventHandler; + type TransitionEventHandler = EventHandler; // // Props / DOM Attributes // ---------------------------------------------------------------------- + /** + * @deprecated. This was used to allow clients to pass `ref` and `key` + * to `createElement`, which is no longer necessary due to intersection + * types. If you need to declare a props object before passing it to + * `createElement` or a factory, use `ClassAttributes`: + * + * ```ts + * var b: Button; + * var props: ButtonProps & ClassAttributes