I maintain a library for React and Preact. The library consists of a function that returns a class component. With React plus @types/react, each instance of the factory receives a generic instance of the React Component interface with my additions. With Preact, the TypeScript compiler complains about a private name:
[ts] Exported variable 'Block' has or is using private name 'JsxstyleComponent'.
Itād be super neat if Preactās Component type definition behaved the same way that Reactās Component type definition did in this regard.
The relevant bits of the factory function:
function factory(displayName: string, defaultProps?: any) {
// ...
return class JsxstyleComponent extends preact.Component<any> {
//...
}
}
const Block = factory('Block', { display: 'block' });
export const Block;
Output type defs from the React version:
export declare const Block: {
new <P>(props: JsxstyleProps<P>): {
className: string | null;
component: AnyComponent<JsxstyleProps<P>>;
componentWillReceiveProps(props: JsxstyleProps<P>): void;
render(): JSX.Element;
setState<K extends never>(f: (prevState: Readonly<{}>, props: JsxstyleProps<P>) => Pick<{}, K>, callback?: (() => any) | undefined): void;
setState<K extends never>(state: Pick<{}, K>, callback?: (() => any) | undefined): void;
forceUpdate(callBack?: (() => any) | undefined): void;
props: Readonly<{
children?: React.ReactNode;
}> & Readonly<JsxstyleProps<P>>;
state: Readonly<{}>;
context: any;
refs: {
[key: string]: React.ReactInstance;
};
};
defaultProps: Dict<React.ReactText> | undefined;
displayName: string;
};
For Preact, I have to manually define a JsxstyleComponent type.
The referenced functions live here:
Iām still wrapping my head around TypeScript but Iām going to do some digging/reading so I can whip up a PR.
PR would be awesome for this, I have no idea what's going on haha.
@developit haha, same š Iām still getting to the bottom of this, but if/when I do Iāll beam you a PR.
@meyer We just merged some exciting updates to the TypeScript typings into master. Can check if these changes fixed your problem? š
@marvinhagemeister will do!
@marvinhagemeister I finally got a second to take a look, and it looks like my original issue has been fixed with the updated types. Thanks!
@meyer That's awesome! We're currently in the process of cutting a new release š
I've opened a request to fix issues like these in TypeScript by bringing declaration files to parity with language features. https://github.com/microsoft/TypeScript/issues/35822