Typescript показывает ошибку после обертывания компонента React с withStyles и withTranslation HOC
Я пытаюсь написать компонент React с помощью Typescript, withStyles
из @material-ui/core
а также withTranslation
из react-i18next
. Обе эти обертки HOC.
Typescript жалуется на ошибку с типами.
Почему-то не работает эта строчка:
export default withTranslation()(withStyles(styles)(HomePage)); // error
Однако, если я использую только withStyles или только withTranslation, он отлично работает:
export default withTranslation()(HomePage); // ok
export default withStyles(styles)(HomePage); // ok
Я знаю, что есть проблема с типами, интерфейсами или, может быть, что-то не так с экспортом моих компонентов таким образом. У меня была такая же проблема, когда я использовалwithRouter
вместе с withStyles
. Я решил ошибку, изменив их порядок в строке экспорта! (Не понял, почему это было решено TBH) Я видел некоторые решения, которые предлагают обернуть весь компонент подwithStyles
но мне это кажется некрасивым. Есть какие-нибудь предложения?
Это ошибка:
(alias) withStyles<"root", {}, {}>(style: Styles<Theme, {}, "root">, options?: {} | undefined): PropInjector<{
classes: Record<"root", string>;
}, StyledComponentProps<"root">>
import withStyles
Argument of type 'ComponentType<Pick<IProps, "i18n" | "t"> & StyledComponentProps<"root">>' is not assignable to parameter of type 'ComponentType<WithTranslation>'.
Type 'ComponentClass<Pick<IProps, "i18n" | "t"> & StyledComponentProps<"root">, any>' is not assignable to type 'ComponentType<WithTranslation>'.
Type 'ComponentClass<Pick<IProps, "i18n" | "t"> & StyledComponentProps<"root">, any>' is not assignable to type 'ComponentClass<WithTranslation, any>'.
Types of property 'propTypes' are incompatible.
Type 'WeakValidationMap<Pick<IProps, "i18n" | "t"> & StyledComponentProps<"root">> | undefined' is not assignable to type 'WeakValidationMap<WithTranslation> | undefined'.
Type 'WeakValidationMap<Pick<IProps, "i18n" | "t"> & StyledComponentProps<"root">>' is not assignable to type 'WeakValidationMap<WithTranslation>'.
Types of property 'i18n' are incompatible.
Type 'Validator<{ changeLanguage: Function; }> | undefined' is not assignable to type 'Validator<i18n> | undefined'.
Type 'Validator<{ changeLanguage: Function; }>' is not assignable to type 'Validator<i18n>'.
Type '{ changeLanguage: Function; }' is missing the following properties from type 'i18n': t, init, loadResources, use, and 28 more.ts(2345)
index.d.ts(80, 3): 'tReady' is declared here.
Это моя составляющая:
import React, { PureComponent } from "react";
// i18n
import { withTranslation } from "react-i18next";
import { InterfaceI18n } from "../../i18n";
// Stylus
import { WithStyles } from "@material-ui/core";
import withStyles from "@material-ui/core/styles/withStyles";
import styles from "./HomePage.style";
type WrapperPropsStylus = WithStyles<typeof styles>;
interface IProps extends InterfaceI18n, WrapperPropsStylus {}
class HomePage extends PureComponent<IProps> {
changeLanguage = (lng: string) => {
const { i18n } = this.props;
i18n.changeLanguage(lng);
};
public render() {
const { classes, t } = this.props;
return (
<div className={classes.root}>
<div>
This is the home page (Public page) <br />
</div>
{t("Welcome to React")} <br />
<button onClick={() => this.changeLanguage("de")}>de</button>
<button onClick={() => this.changeLanguage("en")}>en</button>
</div>
);
}
}
export default withTranslation()(withStyles(styles)(HomePage));
1 ответ
Итак, чтобы решить эту проблему, я изменил компонент на хуки. Просто используйте хуки вместо компонентов классов, с TS намного проще.