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.

[bug?] withStyles not working as a regular function in TypeScript.

See original GitHub issue

EDIT: I was trying to use it as a decorator, which the docs say isn’t supported, but even as a function I get the same error (below)

Seems like this was supposed to be fixed back in https://github.com/mui-org/material-ui/pull/8178, but I’m on @material-ui/core 3.9.0 and typescript 3.1.6 and it isn’t working.

F.e.

@withStyles(baseStyles)
export default class LayerManager extends React.Component<{...}, {...}> {

gives the error

screen shot 2019-01-16 at 7 23 52 pm

error TS2345: Argument of type 'typeof LayerManager' is not assignable to parameter of type 'ComponentType<never>'.
[1]   Type 'typeof LayerManager' is not assignable to type 'ComponentClass<never, any>'.
[1]     Type 'LayerManager' is not assignable to type 'Component<never, any, any>'.
[1]       Types of property 'props' are incompatible.
[1]         Type 'Readonly<{ children?: ReactNode; }> & Readonly<ILayerManagerProperties>' is not assignable to type 'never'.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
trusktrcommented, May 13, 2019

I found another way to fix it. I can also replace

  constructor(props, context) {
    super(props, context)
    this.state = {}
  }

with

  constructor(props, context?) {
    super(props, context)
    this.state = {}
  }

Notice the only difference is the question mark ?.

The error that we’re getting leaves us with no clue as to what’s happening. Is there some way to make the error more helpful, so that it may tell us that we’ve extended the constructor improperly?

0reactions
eps1loncommented, May 14, 2019

@trusktr context needs to be optional in the constructor. This is implied in the react typings. not assignable to never usually indicates that you passed the wrong type.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[bug?] withStyles not working as a regular function in TypeScript.
EDIT: I was trying to use it as a decorator, which the docs say isn't supported, but even as a function I get...
Read more >
Material-ui-next withStyles decorator doesn't work correctly
I'm rewriting my little project to material-ui, and I'm using for this material-ui-next reactjs library; For one component withStyle ...
Read more >
Typing React with typescript - DEV Community ‍ ‍
Since we use MUI we handle styling with JSS and the generated CSS classes are injected via the withStyles HOC. This caused some...
Read more >
Documentation - Do's and Don'ts - TypeScript
❌ Don't ever use the types Number , String , Boolean , Symbol , or Object These types refer to non-primitive boxed objects...
Read more >
Using styled-components in TypeScript: A tutorial with examples
Here, you can learn how to build and style a TypeScript app using styled-components, with an e-commerce page as an example.
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