TypeScript:“字符串|未定义”不能分配给“字符串”类型

时间:2018-11-07 08:48:32

标签: reactjs typescript material-ui typescript-typings typescript-types

我正在使用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组装导致我的应用程序崩溃。

2 个答案:

答案 0 :(得分:1)

问题是,对象的属性可以是未定义的,在这种情况下,您输入prop需要一个字符串,因此,解决方法是:

classes={{
   disabled: classes.disabled,
   input: inputClasses,
   root: marginTop,
   underline: underlineClasses || ''
}}

答案 1 :(得分:1)

发现我自己的错误‍♂️我不小心又写了<Input />而不是<MUIInput />

<MUIInput
  classes={{
    disabled: classes.disabled,
    input: inputClasses,
    root: marginTop,
    underline: underlineClasses
  }}
  id={id}
  {...inputProps}
/>