Skip to content

Commit

Permalink
std: Provide types for React and ReactDOM (denoland#4376)
Browse files Browse the repository at this point in the history
Introduces `std/types` which is designed to provide types for common
libraries that are compatible with Deno.
  • Loading branch information
kitsonk committed Mar 17, 2020
1 parent 9833975 commit 9050d36
Show file tree
Hide file tree
Showing 10 changed files with 37,391 additions and 0 deletions.
28 changes: 28 additions & 0 deletions std/types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# std/types

Contains types for popular external packages that are compatible with Deno.

Because Deno only resolves fully qualified file names, type definitions that
import other type definitions might not work with Deno. Also, when some type
definition supply some global interfaces, they can conflict with Deno. The types
located here have been validated to work with Deno.

The types that are currently available:

- `react.d.ts` - For React 16. Sources known to work well for Deno:
- Pika CDN: `https://cdn.pika.dev/_/react/v16`
- JSPM: `https://dev.jspm.io/react@16`
- `react-dom.d.ts` - For ReactDOM 16. Sources known to work well for Deno:
- Pika CDN: `https://cdn.pika.dev/_/react-dom/v16`
- JSPM: `https://dev.jspm.io/react-dom@16`

There are several ways these type definitions can be referenced. Likely the
"best" way is that the CDN provider provides a header of `X-TypeScript-Types`
which points to the type definitions. We are working to have this available, but
currently you would need to use the compiler hint of `@deno-types`. For example
to import React:

```ts
// @deno-types="https://deno.land/std/types/react.d.ts"
import React from "https://cdn.pika.dev/_/react/v16";
```
125 changes: 125 additions & 0 deletions std/types/react-dom.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// These types are adapted from
// https://github.com/DefinitelyTyped/DefinitelyTyped to work under Deno.
//
// Type definitions for React (react-dom) 16.9
// Project: http:https://facebook.github.io/react/
// Definitions by: Asana <https://asana.com>
// AssureSign <http:https://www.assuresign.com>
// Microsoft <https://microsoft.com>
// MartynasZilinskas <https://github.com/MartynasZilinskas>
// Josh Rutherford <https://github.com/theruther4d>
// Jessica Franco <https://github.com/Jessidhia>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8

// NOTE: Users of the `experimental` builds of React should add a reference
// to 'react-dom/experimental' in their project. See experimental.d.ts's top comment
// for reference and documentation on how exactly to do it.

/* eslint-disable */

export as namespace ReactDOM;

import {
ReactInstance,
Component,
ComponentState,
ReactElement,
SFCElement,
CElement,
DOMAttributes,
DOMElement,
ReactNode,
ReactPortal
} from "./react.d.ts";

export function findDOMNode(
instance: ReactInstance | null | undefined
): Element | null | Text;
export function unmountComponentAtNode(container: Element): boolean;

export function createPortal(
children: ReactNode,
container: Element,
key?: null | string
): ReactPortal;

export const version: string;
export const render: Renderer;
export const hydrate: Renderer;

export function unstable_batchedUpdates<A, B>(
callback: (a: A, b: B) => any,
a: A,
b: B
): void;
export function unstable_batchedUpdates<A>(callback: (a: A) => any, a: A): void;
export function unstable_batchedUpdates(callback: () => any): void;

export function unstable_renderSubtreeIntoContainer<T extends Element>(
parentComponent: Component<any>,
element: DOMElement<DOMAttributes<T>, T>,
container: Element,
callback?: (element: T) => any
): T;
export function unstable_renderSubtreeIntoContainer<
P,
T extends Component<P, ComponentState>
>(
parentComponent: Component<any>,
element: CElement<P, T>,
container: Element,
callback?: (component: T) => any
): T;
export function unstable_renderSubtreeIntoContainer<P>(
parentComponent: Component<any>,
element: ReactElement<P>,
container: Element,
callback?: (component?: Component<P, ComponentState> | Element) => any
): Component<P, ComponentState> | Element | void;

export interface Renderer {
// Deprecated(render): The return value is deprecated.
// In future releases the render function's return type will be void.

<T extends Element>(
element: DOMElement<DOMAttributes<T>, T>,
container: Element | null,
callback?: () => void
): T;

(
element: Array<DOMElement<DOMAttributes<any>, any>>,
container: Element | null,
callback?: () => void
): Element;

(
element: SFCElement<any> | Array<SFCElement<any>>,
container: Element | null,
callback?: () => void
): void;

<P, T extends Component<P, ComponentState>>(
element: CElement<P, T>,
container: Element | null,
callback?: () => void
): T;

(
element: Array<CElement<any, Component<any, ComponentState>>>,
container: Element | null,
callback?: () => void
): Component<any, ComponentState>;

<P>(
element: ReactElement<P>,
container: Element | null,
callback?: () => void
): Component<P, ComponentState> | Element | void;

(element: ReactElement[], container: Element | null, callback?: () => void):
| Component<any, ComponentState>
| Element
| void;
}
Loading

0 comments on commit 9050d36

Please sign in to comment.