score:4
what this means is that the declared type of the @withstyles
decorator:
export default function withstyles<p = {}, classnames = {}>(
style: stylerules | stylerulescallback,
options?: withstylesoptions
): (
component: react.componenttype<p & { classes: classnames; theme?: theme }>
) => react.componentclass<p & styledcomponentprops<classnames>>;
doesn't actually meet the contract that typescript needs for class decorators, which is something more like:
declare function classdecorator(...args: any[]): <c> (c: c) => c;
the contract is pretty strict: it requires that the function output be a subtype of the input. this has led people to have problems before.
if everything is working fine at runtime and you just want to silence the type checker, there's always good old any
:
@(withstyles as any)(styles) // no error now
class mycomponent extends react.component<any, any> {
manager: any;
...
}
or, you could go ahead and try to re-declare the type of withstyles()
to be more friendly to decorators:
import { styledcomponentprops } from 'material-ui';
import { theme } from 'material-ui/styles/createmuitheme';
import { stylerules, stylerulescallback, withstylesoptions } from 'material-ui/styles/withstyles';
import { withstyles } from 'material-ui/styles'
declare module 'material-ui/styles' {
export function withstyles(
style: stylerules | stylerulescallback,
options?: withstylesoptions
): <c extends react.componenttype<p & { classes: classnames; theme?: theme }>, p = {}, classnames = {}> (
component: c
) => c & react.componentclass<p & styledcomponentprops<classnames>>;
}
@withstyles(styles) // no error now
class mycomponent extends react.component<any, any> {
manager: any;
}
now everything works because i ensure that the returned value is a subtype of the mycomponent
constructor. it's a lot of someone else's code to fix in your own project; i'd probably just use the any
solution myself.
whether someone wants to contact the material ui folks and suggest updating the definition is up to them. i don't consider myself knowledgeable enough on react et al. to understand if my altered declaration is appropriate.
anyway, hope that helps; good luck!
score:1
i wasn't able to use @jcalz's type definition since it conflicts with the existing one, so i made a wrapper function.
having classes
be required on withstyles
also makes it a pain to use decorated components, so i make it optional instead.
// withstyles.ts
import { theme } from 'material-ui/styles';
import _withstyles, {
classnamemap,
styledcomponentprops,
stylerules,
stylerulescallback,
withstylesoptions,
} from 'material-ui/styles/withstyles';
export interface withstyles<classkey extends string = string> {
classes?: classnamemap<classkey>;
theme?: theme;
}
// we need to fix the withstyles definition and we want to make withstyles.classes optional,
// so we make our own.
export const withstyles = <classkey extends string>(
style: stylerules<classkey> | stylerulescallback<classkey>,
options?: withstylesoptions
) => <c extends react.componenttype<p & withstyles<classkey>>, p = {}>(component: c) => {
return (_withstyles as any)(style, options)(component) as c & react.componenttype<p & styledcomponentprops<classkey>>;
};
to use it just import the new withstyles.ts
instead of the material-ui
one.
import { withstyles, withstyles } from './withstyles';
@withstyles(styles)
export class myclass extends react.component<myclassprops> {
render() {
// classes will always exist so use `!`.
const classes = this.props.classes!;
return (
<div classname={classes.root} />
);
}
}
Source: stackoverflow.com
Related Query
- Cryptic Typescript compiler error using React: 'ComponentClass<StyledComponentProps<{}>>' is not assignable to type 'typeof MyComponent'
- Typescript error when using React useContext and useReducer
- Error when passing parameters in components in react using Typescript
- TypeScript compiler error with React Stateless Function component
- Setup Jest + React testing library using NextJS in TypeScript -- error setting upp jest.config.js
- Error coming when using the state value in react using typescript
- Error using css modules in typescript react with webpack config
- Error rendering react component using Typescript and WebPack
- Object is of type unknown error when using Typescript with React
- Error Coming when passing props from one component to another and vice - versa in react using typescript
- Error in typescript using react framer Type '() => void' is not assignable to type 'undefined'
- Typescript error showing up when update method for state element by using React useState
- Getting Typescript Error while adding object to array using useState in React
- Typescript throws an explicit function return type error when using Suspense with React Router
- React - using forwardRef in component, typescript error - Property 'current' is missing
- TypeScript & React/JSX: tsc compiler error when supplying props to react component's superclass
- Trying to deploy React App to Azure using a Github Actions workflow but getting an error in TypeScript
- Typescript compiler errors when using with Context Provider API - React
- Typescript ESLint Error - How can I type children when using react functional components?
- React createElement error when trying to import in another class using TypeScript
- Apollo react typescript error when using destructured useMutation function in onClick
- Prop TypeScript Error With React Component, Using Styled-Components
- Typescript error TS2554: Expected 0 arguments, but got 1. while using useReducer and useContext with React
- Getting error while using useDrag({}) method in react typescript with react-dnd
- How to fix the error react hook cannot be called inside a callback function using typescript and react?
- react using a param as function strict mode typescript error
- How to declare a variable before assign it in React using Typescript without "Index signature is missing in type" Error
- React Class is throwing a compile error regading function classs using TypeScript
- Default property value in React component using TypeScript
- Cannot find namespace 'ctx' error when creating Context with react - typescript
More Query from same tag
- Which is the best practise for using axios in React Project
- How to get specific fields from an api fetch using redux saga
- IPFS issue: can't upload files with ipfs.add(file)
- Ask user for confirmation before updating form value in final-form
- Live searching of external data based on any character - React Hooks
- Meteor-react ES6 Class React.Component
- setAttribute onClick function - React JS
- ipcRenderer in React component constructor
- React library with typescript and module css
- DefinitelyTyped: Add new props typing for existing component
- One function does not work inside another in React Native
- Can I disable TypeScript Inference for imported JS Modules? (Or config it to always infer type: any)
- getting error when I use destructuring assignment to simplify my props
- How to read value from unknown type?
- fire redux action from extraReducers
- Unable to show helperText once we click on the Submit button using reactjs?
- Attempted import error: 'HelpBlock' is not exported from 'react-bootstrap'
- Lopping over arrays in typescript
- Redirect to a specific url with different query params using useHistory hook
- how to get value of Material-ui auto-complete in react.js
- Is duplicating state of props the only way to go in React.js?
- Firebase Realtime Database validations rules not working
- React memory leak warning
- useState Hook inside loop or conditional statement
- How to display a single product?
- Private Routing is not getting set in React App
- Using base state with hooks in React
- Trying to reflect changes in Redux State from CurrentUser on React DOM for CurrentUser?
- On react-motion, how to reset an animation?
- Antd how to do onClick on List or Card action?