Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
meyer opened this issue Nov 29, 2017 · 7 comments

Comments

@meyer
Copy link

meyer commented Nov 29, 2017

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.

@developit
Copy link
Member

PR would be awesome for this, I have no idea what's going on haha.

@meyer
Copy link
Author

meyer commented Dec 4, 2017

@developit haha, same 😓 I’m still getting to the bottom of this, but if/when I do I’ll beam you a PR.

@marvinhagemeister
Copy link
Member

@meyer We just merged some exciting updates to the TypeScript typings into master. Can check if these changes fixed your problem? 🎉

@meyer
Copy link
Author

meyer commented Mar 4, 2018

@marvinhagemeister will do!

@meyer
Copy link
Author

meyer commented 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!

@meyer meyer closed this as completed Apr 24, 2018
@marvinhagemeister
Copy link
Member

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

@trusktr
Copy link

trusktr commented Dec 21, 2019

I've opened a request to fix issues like these in TypeScript by bringing declaration files to parity with language features. microsoft/TypeScript#35822

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants