reactjs - TypeScript: 'string | undefined' 不可分配给类型 'string'

标签 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总是返回一个字符串(即使它是空的)。此外,classes.disabled 也始终被定义,因为它包含在我的 styles 中。 .

我该如何解决这个问题?我在严格模式下开发,所以这个 linter 故障导致我的应用程序崩溃。

最佳答案

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

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

关于reactjs - TypeScript: 'string | undefined' 不可分配给类型 'string',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53186049/

相关文章:

javascript - React : TypeError: Object(. ..) 不是 Material UI 中的函数

typescript - 如何在 VS2017 中使用通过 NuGet 安装的 Typescript 定义(类型)文件?

typescript - Visual Studio 2022 Typescript 智能感知

reactjs - 为什么 TypeScript 中 material-ui Button 元素的 React.createRef() 无法正常工作?

reactjs - 在 React JS 中使用异步方法加载本地 JSON 文件

reactjs - 更改所有 Material UI 组件的字体系列

javascript - 如何将数据从子组件(子组件有自己的状态)传递给父组件?

javascript - 如何使用 Vue 类组件访问 VueJS 3 和 Typescript 中的 HTML 引用?

reactjs - 如何在 React 中滚动 Swipeabledrawer MUI

javascript - 从 JSON 对象生成 Material 时间线