[英]TypeScript: 'string | undefined' is not assignable to type 'string'
我正在使用TypeScript編寫React應用程序。 我將material-ui用於組件。 我目前正在為material-ui的輸入編寫包裝器,如下所示:
import FormControl, { FormControlProps } from "@material-ui/core/FormControl";
import MUIInput, { InputProps } from "@material-ui/core/Input";
import InputLabel, { InputLabelProps } from "@material-ui/core/InputLabel";
import withStyles, { WithStyles } from "@material-ui/core/styles/withStyles";
import classNames from "classnames";
import React, { PureComponent, ReactNode } from "react";
import styles from "./styles";
export interface OwnProps {
labelText?: ReactNode;
labelProps?: InputLabelProps;
id?: string;
inputProps?: InputProps;
formControlProps?: FormControlProps;
inputRootCustomClasses?: string;
success?: boolean;
white?: boolean;
error?: boolean;
}
export interface Props extends WithStyles<typeof styles>, OwnProps {}
export class Input extends PureComponent<Props> {
render() {
const {
classes,
formControlProps,
labelText,
id,
labelProps,
inputProps,
error,
white,
inputRootCustomClasses,
success
} = this.props;
const labelClasses = classNames({
[" " + classes.labelRootError]: error,
[" " + classes.labelRootSuccess]: success && !error
});
const underlineClasses = classNames({
[classes.underlineError]: error,
[classes.underlineSuccess]: success && !error,
[classes.underline]: true,
[classes.whiteUnderline]: white
});
const marginTop = classNames({
[inputRootCustomClasses!]: inputRootCustomClasses !== undefined
});
const inputClasses = classNames({
[classes.input]: true,
[classes.whiteInput]: white
});
let formControlClasses;
if (formControlProps !== undefined) {
formControlClasses = classNames(formControlProps.className, classes.formControl);
} else {
formControlClasses = classes.formControl;
}
return (
<FormControl {...formControlProps} className={formControlClasses}>
{labelText !== undefined ? (
<InputLabel
className={classes.labelRoot + " " + labelClasses}
htmlFor={id}
{...labelProps}
>
{labelText}
</InputLabel>
) : null}
<Input
classes={{
disabled: classes.disabled,
input: inputClasses,
root: marginTop,
underline: underlineClasses
}}
id={id}
{...inputProps}
/>
</FormControl>
);
}
}
export default withStyles(styles)(Input);
我對此<Input />
的屬性有疑問:
classes={{
disabled: classes.disabled,
input: inputClasses,
root: marginTop,
underline: underlineClasses
}}
對於禁用,inputt引發錯誤:
[ts]
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
我不知道如何解決這個問題。 我嘗試as
:
underline: underlineClasses as string
不起作用 我嘗試使用!
運算符來斷言非空,但它不起作用。 最奇怪的是,函數classNames
總是返回一個字符串(即使它為空)。 此外,class.disabled也總是被定義,因為它包含在我的styles
。
我該如何解決? 我正在嚴格模式下進行開發,因此此linter組裝會使我的應用程序崩潰。
問題是,對象上的屬性可以是不確定的,在這種情況下,您輸入prop需要一個字符串,因此一種解決方法是:
classes={{
disabled: classes.disabled,
input: inputClasses,
root: marginTop,
underline: underlineClasses || ''
}}
發現我自己的錯誤🤦🏻♂️我不小心又寫了<Input />
而不是<MUIInput />
。
<MUIInput
classes={{
disabled: classes.disabled,
input: inputClasses,
root: marginTop,
underline: underlineClasses
}}
id={id}
{...inputProps}
/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.