question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Function that returns Preact component causes TypeScript “private name” error

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
marvinhagemeistercommented, Apr 26, 2018

@meyer That’s awesome! We’re currently in the process of cutting a new release 🎉

1reaction
meyercommented, Apr 24, 2018

@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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found