Preact: Function that returns Preact component causes TypeScript ā€œprivate nameā€ error

Created on 29 Nov 2017  Ā·  7Comments  Ā·  Source: preactjs/preact

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.

All 7 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

simonjoom picture simonjoom  Ā·  3Comments

paulkatich picture paulkatich  Ā·  3Comments

kossnocorp picture kossnocorp  Ā·  3Comments

matthewmueller picture matthewmueller  Ā·  3Comments

k15a picture k15a  Ā·  3Comments