callstack / react-theme-provider

A set of utilities that help you create your own React theming system in few easy steps
MIT License
466 stars 54 forks source link

TS doesn't see props after «styled()» of «withTheme(styled.button`...`)» #130

Open Opty1712 opened 2 years ago

Opty1712 commented 2 years ago

Linking with https://github.com/callstack/linaria/issues/893

Environment

Description

export const Select = () => (
    <OptionButton type="button" data-qa-id="listItem-btn" />
);

const Button = withTheme(styled.button<Theme>`
    width: 100%;
`);

export const OptionButton = styled(Button)`
    width: 50%;
`;

image

image

If I add Button directly - no error image

If I remove withTheme - no error too image

Reproducible Demo

https://codesandbox.io/s/wild-grass-g4e0u?file=/src/App.tsx

R4YM3 commented 2 years ago

@Opty1712 did you found a work around for this?

R4YM3 commented 2 years ago

Yesterday i took some time to debug this and found what is going on.

You have:

export const Select = () => (
    <OptionButton type="button" data-qa-id="listItem-btn" />
);

const Button = withTheme(styled.button<Theme>`
    width: 100%;
`);

export const OptionButton = styled(Button)`
    width: 50%;
`;

It looks that your Theme type is (when looking at the error)

interface Theme {
    type: string;
    "data-ga-id": string;
}

You need to make another type:

interface IWithTheme {
    theme: Theme;
}

and pass this on:

const Button = withTheme(styled.button<IWithTheme>`
    width: 100%;
`);

It wasn't obvious for me aswell, but withTheme HOC the theme is set in the theme prop. Which is ofcourse logical, since you could also pass other props.