Function that returns Preact component causes TypeScript “private name” error
See original GitHub issueI 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.
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
API - esbuild
private-name -will-throw △ [WARNING] Writing to getter-only property "#foo" will throw [private-name-will-throw] example.js:1:39: 1 │ class Foo { get ...
Read more >Writing a React higher-order component with TypeScript-Reactjs
const hoc = function<TChildProps>(): (component: Component<TChildProps>) ... to TypeScript Causes Error: "Exported variable has or is using private name" ...
Read more >Source - GitHub
The private name syntax can now be parsed, printed, and minified correctly. Transforming this syntax for older browsers is not supported yet.
Read more >@netlify/esbuild-freebsd-arm64 | Yarn - Package Manager
The FreeBSD ARM 64-bit binary for esbuild, a JavaScript bundler. ... which would cause them to become invalid (since the #private name is...
Read more >preactjs - Bountysource
Error : Usage of undeclared private name ... functionality to build static html that could run without javascript? ... It's a project with...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@meyer That’s awesome! We’re currently in the process of cutting a new release 🎉
@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!