简体   繁体   English

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

[英]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.

相关问题 打字稿:类型“字符串”不可分配给类型 - Typescript: Type 'string' is not assignable to type 类型“字符串”不可分配给类型“未定义” - Type 'string' is not assignable to type 'undefined' 类型“字符串”不可分配给类型“未定义” - Type 'string' is not assignable to type ' undefined' 字符串类型别名不可分配给带有 typescript 的 Recoil 中的字符串 - string type alias is not assignable to string in Recoil with typescript 反应/打字稿&#39;字符串| 即使我已经检查了类型,undefined&#39; 也不能分配给类型 &#39;string&#39; - react/typescript 'string | undefined' is not assignable to type 'string' even I already checked the type 如何修复未定义类型的错误参数不可分配给'字符串或()=&gt;字符串类型的参数使用typescript并做出反应? - How to fix error argument of type undefined is not assignable to parameter of type 'string or () => string usng typescript and react? 如何使用 typescript 修复字符串类型的错误参数或未定义的参数不可分配给字符串类型的参数并做出反应? - How to fix the error argument of type string or undefined is not assignable to parameter of type string using typescript and react? 打字稿:类型“字符串”不可分配给自定义类型 - Typescript: Type 'string' is not assignable to custom type TypeScript 错误:类型“字符串”不可分配给排版类型 - TypeScript error: Type 'string' is not assignable to type for Typography React TypeScript:类型“string []”不可分配给类型“never []” - React TypeScript: Type 'string[]' is not assignable to type 'never[]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM