[英]TypeScript: 'string | undefined' is not assignable to type 'string'
I'm writing a React application using TypeScript. 我正在使用TypeScript编写React应用程序。 I use material-ui for my components. 我将material-ui用于组件。 I'm currently writing a wrapper for material-ui's input like this: 我目前正在为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);
I have a problem with this <Input />
's properties: 我对此<Input />
的属性有疑问:
classes={{
disabled: classes.disabled,
input: inputClasses,
root: marginTop,
underline: underlineClasses
}}
For disabled, inputt throws the error: 对于禁用,inputt引发错误:
[ts]
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
I can't figure out how to solve this. 我不知道如何解决这个问题。 I tried as
: 我尝试as
:
underline: underlineClasses as string
Doesn't work. 不起作用 I tried using the !
我尝试使用!
operator to assert not-null, but it doesn't work. 运算符来断言非空,但它不起作用。 The weirdest thing about it is that the function classNames
always returns a string (even if its empty). 最奇怪的是,函数classNames
总是返回一个字符串(即使它为空)。 Furthermore classes.disabled is also always defined, since its included within my styles
. 此外,class.disabled也总是被定义,因为它包含在我的styles
。
How can I solve this? 我该如何解决? I'm developing in strict mode so this linter hickup crashes my application. 我正在严格模式下进行开发,因此此linter组装会使我的应用程序崩溃。
The thing is, a property on a object can be undefined and you input prop in this case needs a string so, a way to fix is: 问题是,对象上的属性可以是不确定的,在这种情况下,您输入prop需要一个字符串,因此一种解决方法是:
classes={{
disabled: classes.disabled,
input: inputClasses,
root: marginTop,
underline: underlineClasses || ''
}}
Found my own error 🤦🏻♂️ I accidentally wrote <Input />
again instead of <MUIInput />
. 发现我自己的错误🤦🏻♂️我不小心又写了<Input />
而不是<MUIInput />
。
<MUIInput
classes={{
disabled: classes.disabled,
input: inputClasses,
root: marginTop,
underline: underlineClasses
}}
id={id}
{...inputProps}
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.