question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add static properties to component using TypeScript

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
tbranyencommented, Jan 10, 2019

@probablyup no worries (I can empathize), it is possible someone will find this closed issue and comment with an answer too 😉

0reactions
probablyupcommented, Jan 10, 2019

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:

I will say it’s rather frustrating to use a library that promotes and touts TypeScript support and has near “official” typings, but then to be anti-questions when the documentation is not sufficient. I hope I find an answer through the DefinitelyTyped issue tracker, because I really think this is the most googleable and relevant location for the question.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/styled-components/styled-components/issues/2317#issuecomment-453279014, or mute the thread https://github.com/notifications/unsubscribe-auth/AAiy1hEheouddqJ8OWRp51j02xGQyCuFks5vB737gaJpZM4Z6bN_ .

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found