Skip to content

Instantly share code, notes, and snippets.

@Silic0nS0ldier
Created March 4, 2018 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Silic0nS0ldier/3c379367b5e6b1abd76e4a41d1be8217 to your computer and use it in GitHub Desktop.
Save Silic0nS0ldier/3c379367b5e6b1abd76e4a41d1be8217 to your computer and use it in GitHub Desktop.
type issue - preact-async-route
tests/ts/index.tsx(18,25): error TS2322: Type '{ path: string; component: typeof Router; }' is not assignable to type 'IAsyncRouteProps & ComponentProps<AsyncRoute>'.
Type '{ path: string; component: typeof Router; }' is not assignable to type 'IAsyncRouteProps'.
Types of property 'component' are incompatible.
Type 'typeof Router' is not assignable to type 'ComponentConstructor | FunctionalComponentConstructor'.
Type 'typeof Router' is not assignable to type 'FunctionalComponentConstructor'.
Type 'Router' is not assignable to type 'FunctionalComponent<C>'.
Type 'Router' provides no match for the signature '(props?: C & ComponentProps<FunctionalComponent<C>>, context?: any): Element'.
import { Component, FunctionalComponent } from 'preact';
type ComponentConstructor = {
new<A, B>() : Component<A, B>;
}
type FunctionalComponentConstructor = {
new<C>() : FunctionalComponent<C>;
}
interface IAsyncRouteProps {
component?: ComponentConstructor | FunctionalComponentConstructor;
getComponent?: (
this: AsyncRoute,
url: string,
callback: (component: ComponentConstructor | FunctionalComponentConstructor) => void,
props: any
) => Promise<any> | void;
loading?: () => JSX.Element;
}
export default class AsyncRoute extends Component<IAsyncRouteProps, {}> {
public render(): JSX.Element | null;
}
import { h, render, Component } from 'preact';
import Router from 'preact-router';
import AsyncRoute from '../../';
/**
* This dummy component is used to catch TypeScript
* type issues via the TypeScript compiler.
*/
function componentFetcher(url: string, cb: (c: any) => void, props: any): Promise<any> | void {}
function loadingAnimation(): JSX.Element | any {
return <div></div>;
}
export class Index extends Component<{}, {}> {
public render(): JSX.Element {
return <Router>// ↓ - Type error here
<AsyncRoute path="/" component={Router} />
<AsyncRoute path="/" getComponent={componentFetcher} />
<AsyncRoute path="/" getComponent={componentFetcher} loading={loadingAnimation} />
</Router>;
}
}
@GregRos
Copy link

GregRos commented Mar 4, 2018

Make sure that Router really has a constructor of the appropriate signature. Maybe it takes more value parameters etc. I noticed from the React type definitions that the Component constructor is defined like this:

constructor(props: P, context?: any);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment