Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compat types #1752

Merged
merged 5 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions compat/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as _hooks from '../../hooks';
import * as preact from '../../src';
import { JSXInternal } from '../../src/jsx'
import { ForwardFn } from './internal';
import * as _internal from './internal';
import * as _Suspense from './suspense';

// export default React;
Expand Down Expand Up @@ -43,6 +43,7 @@ declare namespace React {
export import lazy = _Suspense.lazy;

// Compat
export import ForwardFn = _internal.ForwardFn;
export const version: string;

export function createPortal(vnode: preact.VNode, container: Element): preact.VNode<any>;
Expand All @@ -57,13 +58,13 @@ declare namespace React {
export function isValidElement(element: any): boolean;
export function findDOMNode(component: preact.Component): Element | null;

export class PureComponent<P = {}, S = {}> extends preact.Component {
export abstract class PureComponent<P = {}, S = {}> extends preact.Component {
marvinhagemeister marked this conversation as resolved.
Show resolved Hide resolved
isPureReactComponent: boolean;
}

export function memo<P = {}>(component: preact.FunctionalComponent<P>, comparer?: (prev: P, next: P) => boolean): preact.FunctionComponent<P>;

export function forwardRef<P = {}>(fn: ForwardFn<P, any>): preact.FunctionalComponent<P>;
export function forwardRef<R, P = {}>(fn: _internal.ForwardFn<P, R>): preact.FunctionalComponent<P>;

export function unstable_batchedUpdates(callback: (arg?: any) => void, arg?: any): void;

Expand Down
4 changes: 2 additions & 2 deletions compat/src/internal.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ref } from '../..';
import { Ref, ComponentChild } from '../..';
import {
Component as PreactComponent,
VNode as PreactVNode,
Expand Down Expand Up @@ -28,7 +28,7 @@ export interface VNode<T = any> extends PreactVNode<T> {
}

export interface ForwardFn<P = {}, T = any> {
(props: P, ref: Ref<T>): VNode;
(props: P, ref: Ref<T>): ComponentChild;
displayName?: string;
}

Expand Down
6 changes: 4 additions & 2 deletions compat/src/suspense.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component } from '../../src';
import { Component, ComponentChild } from '../../src';

//
// Suspense/lazy
Expand All @@ -10,4 +10,6 @@ export interface SuspenseProps {
fallback: preact.ComponentChildren;
}

export class Suspense extends Component<SuspenseProps> {}
export class Suspense extends Component<SuspenseProps> {
render(): ComponentChild;
}
21 changes: 21 additions & 0 deletions compat/test/ts/forward-ref.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as React from '../../src';

const MyInput: React.ForwardFn<{ id: string }, { focus(): void }> = (props, ref) => {
const inputRef = React.useRef<HTMLInputElement>()

React.useImperativeHandle(ref, () => ({
focus: () => {
if (inputRef.current) {
inputRef.current.focus()
}
}
}))

return <input {...props} ref={inputRef} />
}

export const foo = React.forwardRef(MyInput)

export const Bar = React.forwardRef<HTMLDivElement, { children: any }>((props, ref) => {
return <div ref={ref}>{props.children}</div>
})
4 changes: 2 additions & 2 deletions hooks/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PreactContext } from "../..";
import { PreactContext, Ref as PreactRef } from "../..";

type Inputs = ReadonlyArray<unknown>;

Expand Down Expand Up @@ -74,7 +74,7 @@ type CreateHandle = () => object;
* ref.current
* @param inputs If present, effect will only activate if the values in the list change (using ===).
*/
export function useImperativeHandle<T>(ref: Ref<T>, create: CreateHandle, inputs?: Inputs): void;
export function useImperativeHandle<T, R extends T>(ref: PreactRef<T>, create: () => R, inputs?: Inputs): void;

/**
* Accepts a function that contains imperative, possibly effectful code.
Expand Down