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

Type-hinting functional components with JSX attributes? #1

Open
mindplay-dk opened this issue Mar 7, 2018 · 3 comments
Open

Type-hinting functional components with JSX attributes? #1

mindplay-dk opened this issue Mar 7, 2018 · 3 comments

Comments

@mindplay-dk
Copy link

Thanks for this brilliant source of reference! Really cuts through the noise :-)

What's missing for me, is how to type-hint a functional component that accepts standard JSX attributes?

I tried the following:

import { h, FunctionalComponent } from "preact";

export interface ButtonProps {
    title: string;
}

export const Button: FunctionalComponent<ButtonProps> = props => {
    let { title, ...rest } = props;

    return <button {...rest}>{title}</button>;
};

But I get the following error:

Type '(ButtonProps & ComponentProps<FunctionalComponent<ButtonProps>>) | undefined' has no property 'title' and no string index signature.

Clearly, type-hinting as FunctionalComponent<ButtonProps> isn't the right way to go about this?

@mindplay-dk
Copy link
Author

Like this maybe?

import { h } from "preact";

export interface ButtonProps {
    title: string;
}

export const Button = (props: JSX.HTMLAttributes) => {
    const { title, ...rest } = props;

    return <button {...rest}>{title}</button>;
};

@mindplay-dk
Copy link
Author

Hmm, no, that doesn't give me access to children 🙄

@scurker
Copy link
Owner

scurker commented Mar 12, 2018

Sorry it took me a little while to get around to this, but it looks like preactjs/preact#1033 may have solved the issue? It's not been released yet, but using the updated definition it appears your examples now work.

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

2 participants