Add static properties to component using TypeScript
See original GitHub issueI am trying to add properties to the constructor produced by styled components using TypeScript.
What I want to do:
type JSXButtonProps = JSX.IntrinsicElements['button'];
export interface ButtonProps extends JSXButtonProps {
theme?: ThemeBase;
}
const ButtonInner = ({ theme, ...rest }: ButtonProps) => <button {...rest} />;
export const Button = styled(ButtonInner)(
(props: ButtonProps): CSSEntry => get(props.theme, 'button.extend'),
);
Button.Primary = props => <Button />;
Which produces the error:
error TS2339: Property 'Primary' does not exist on type 'StyledComponent<FunctionComponent<ButtonProps>, ThemeBase, {}, never>'.
What works (casting to any):
export const Button = styled(ButtonInner)(
(props: ButtonProps): CSSEntry => get(props.theme, 'button.extend'),
);
(Button as any).Primary = props => <Button />;
I’d like to avoid this, so I looked into re-creating my own ButtonInterface
, but the typings are so complex, I can’t figure out where to start. For instance I see StyledComponent<...>
but this is a generic type which cannot be extended, so that won’t work. I’ve tried making the type and joining in the additional static class properties, but that hasn’t worked either.
My latest attempt has been to do:
interface ButtonAliases extends StyledComponentBase<any, any, any, any> {
Primary: any;
Secondary: any;
Tertiary: any;
Icon: any;
}
export const Button: ButtonAliases = styled(ButtonInner)(...);
But this does not work either, I get the error:
error TS2322: Type 'StyledComponent<({ theme, ...rest }: ButtonProps) => Element, ThemeBase, {}, never>' is not assignable to type 'ButtonAliases'.
Any help or suggestions would be great, thank you!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:7 (3 by maintainers)
Top Results From Across the Web
React functional component static property - Stack Overflow
Typescript compiler is telling you that you are using a property that is not defined in the function. Move Panel.Fieldset = PanelFieldset ...
Read more >TypeScript Static - TutorialsTeacher
The static members can be defined by using the keyword static. Consider the following example of a class with static property. Example: Static...
Read more >All about TypeScript Static Members | TypeScript OOP
In this blog post, I explain the static keyword and when you might want to make attributes and methods a member of the...
Read more >Functional Components - React And Typescript
Functional Components with "Static" Properties. If you need to use a "static" property with your functional component (say, to define a router), ...
Read more >Static Properties, Abstract Classes, and Constructor Functions ...
This is the same with TypeScript. Classes serve as templates to create new objects. TypeScript extends the syntax of classes of JavaScript and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@probablyup no worries (I can empathize), it is possible someone will find this closed issue and comment with an answer too 😉
I’ll refine the website copy to be a bit more explicit that the typings are non-official, thank you for the feedback. I get your frustration; it’s just difficult to keep a tidy issue tracker when stuff we don’t maintain is in it. On Thu, Jan 10, 2019 at 4:26 PM Tim Branyen notifications@github.com wrote: