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.

TypeScript props interface is cast to never when theme prop is not given and BaseStyledInterface is used

See original GitHub issue

This line (L66) was recently added, and it causes an edge case in a very specific (but common) use case:

https://github.com/styled-components/styled-components/blob/62ae36eeb6ddf43ec2f1d5b8e0546dd8df47b15d/typings/styled-components.d.ts#L65-L68

Here we set the theme prop to be optional when P extends { theme?: T; }. But by default T is any in the exposed BaseStyledInterface.

This causes WithOptionalTheme to exhibit the bug. It then casts P to never. That means that the resulting interface is just: { theme?: any }.

The bug disappears when T is not any, meaning when theme is set to something else.

Here is an image of @JonWallsten’s reproduction: http://imgur.com/a/i1GrD

The interface of ICombinedSpinButton is:

export interface ICombinedSpinButton extends IAutoplayButton {
  autoplayActive?: boolean
  autoplayRunning?: boolean
  hidden?: boolean
}

(Ignore the extend, it’s irrelevant)

Note that the theme property is not cast to a type.

cc @Igorbek

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
kittencommented, Aug 10, 2017

I’ll release a minor version today containing this fix

1reaction
avindracommented, Aug 9, 2017
interface Props extends React.HTMLProps<HTMLImageElement> {
  isOpen?: boolean;
}

const DumbBox =  ({ isOpen }: Props) => (
  <img width={isOpen ? "50px" : "25px"} src="blah" />
);

const StyledDumbBox = styled(DumbBox)`
  color: red;
`;

This will throw an error in StyledDumbBox

Reverting to 2.1.1 fixes the build error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using styled components with Typescript, prop does not exist?
I get warnings that: Property 'emphasized' does not exist on type 'ThemedStyledProps<DetailedHTMLProps<HTMLAttributes<HTMLHeadingElement>, ...
Read more >
Typing defaultProps - React TypeScript Cheatsheets
The consensus is to use object default values. Function Components: type GreetProps = { age ...
Read more >
Defining exclusive properties with TypeScript - miyauci.me
Shows how to define exclusive properties in TypeScript. It explains the background ... In one interface, the property color is used to specify...
Read more >
TypeScript - Material UI - MUI
Many components concerned with user input offer a value prop or event handlers which include the current value . In most situations that...
Read more >
Google TypeScript Style Guide
Do not mark interfaces specially ( IMyInterface or MyFooInterface ) unless it's idiomatic in its environment. When introducing an interface for a class,...
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