Skip to content

10.0.0-rc.3 preact/compat typescript incompatibilities #1930

Description

@bvella

First off, well done on this excellent library. It boggles my mind how smaller this is from react.

I have some existing react libraries and a react app which I am trying out with preact. It is working fine but I had to do some modifications to the typescript typings as below:

I added the following definitions to compat/src/index.d.ts :

export type Component<P = {}, S = {}, SS = any> = preact.Component<P, S>;
export type ReactElement<P = {}, S = any> = preact.VNode<P>;
export import FunctionComponent = preact.FunctionComponent;
export import ComponentClass = preact.ComponentClass;
export import ComponentType = preact.ComponentType;
export import Context = preact.Context;
export import ReactNode = preact.ComponentChildren;
export type CSSProperties = string | {[key: string]: string | number};
export type AllHTMLAttributes<T> = JSX.HTMLAttributes;

I also changed jsx to a global namespace in src/jsx.d.ts:

...
export = JSX;
		
declare global {
export namespace JSX {
...
}
}

I also noticed that there are some subtle types missing, like e.g. the target in the onChange event for an input tag is not typed. I had this jsx line in which the event's target attribute was correctly inferred to be an html input element from the react typings:

<input type="text" name="name" onChange={ e => setName(e.target.value) } />

that I had to change to the following as the preact event handlers all use the same event definition with target being untyped:

<input type="text" name="name" onChange={ e => setName((e.target as HTMLInputElement).value) } />

Are these by design or is there a reason I am not aware of for these missing types? I can go through the react typings and copy over some stuff to improve type checking, if you think this is of benefit to preact. These would be typescript typing updates only, requiring no changes in preact but improving greatly both switching to preact from react and fix subtle type checking errors like the event above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions