【问题标题】:No overload matches this call with styled components (index.d.ts(2187, 9))没有重载将此调用与样式化组件匹配 (index.d.ts(2187, 9))
【发布时间】:2021-09-02 21:18:55
【问题描述】:

我正在尝试使用带有道具的样式组件创建输入组件。当我尝试名为 onChange 的道具时,得到错误 No overload matches this call。 我认为我的类型在某些方面不正确,但是经过数小时的谷歌搜索后,我仍然没有得到答案。 我错过了什么?

interface Input {
  onChange?: (x? : string ) => void,
  disabled?: boolean,
  readonly?: boolean,
  width?: CSSUnit,
  heightlightColor?: string,
  bgColor?: string,
  type?: string,
  placeholder?: string,
  isFocus?: boolean,
  fontSize?: CSSUnit,
  highlightColor?: string,
  padding? : CSSUnit,
  height? : CSSUnit
}

const InputWrapperDefault = styled.input<Input>`
  background-color: inherit;
  width: 100%;
  height: 100%;
  outline: none;
  border-width: 0px;
  color: inherit;
  font-size: ${(props) => props.fontSize || '16px' };
  padding: ${(props => props.padding ? props.padding : '16px')};
  box-sizing: border-box;
`

const Input: React.FC<Input> = (props) => {
  const [isFocus, setFocus] = useState(false);
  const {
    highlightColor,
    onChange = ()=>{},
    type = 'text',
    width,
    bgColor,
    ...restProps
  } = props;
  console.log(isFocus);
  return (
        <InputWrapperDefault
          autoCapitalize="sentences"
          autoComplete="on"
          autoCorrect="on"
          dir="auto"
          maxLength={50}
          name='name'
          onChange={(e:ChangeEvent<HTMLInputElement>):void => { onChange(e.target.value)} }
          onClick={()=> setFocus(true)}
          spellCheck="true"
          type={type}
          {...restProps}
        />
  )
}

(JSX attribute) onChange?: (React.ChangeEventHandler<HTMLInputElement> & ((x?: string | undefined) => void)) | undefined
No overload matches this call.
  Overload 1 of 2, '(props: Omit<Omit<Pick<DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>, "key" | keyof InputHTMLAttributes<...>> & { ...; } & Input, never> & Partial<...>, "theme"> & { ...; } & { ...; }): ReactElement<...>', gave the following error.
    Type '(e: ChangeEvent<HTMLInputElement>) => void' is not assignable to type 'ChangeEventHandler<HTMLInputElement> & ((x?: string | undefined) => void)'.
      Type '(e: ChangeEvent<HTMLInputElement>) => void' is not assignable to type '(x?: string | undefined) => void'.
        Types of parameters 'e' and 'x' are incompatible.
          Type 'string | undefined' is not assignable to type 'ChangeEvent<HTMLInputElement>'.
            Type 'undefined' is not assignable to type 'ChangeEvent<HTMLInputElement>'.
  Overload 2 of 2, '(props: StyledComponentPropsWithAs<"input", any, Input, never, "input", "input">): ReactElement<StyledComponentPropsWithAs<"input", any, Input, never, "input", "input">, string | JSXElementConstructor<...>>', gave the following error.
    Type '(e: ChangeEvent<HTMLInputElement>) => void' is not assignable to type 'ChangeEventHandler<HTMLInputElement> & ((x?: string | undefined) => void)'.
      Type '(e: ChangeEvent<HTMLInputElement>) => void' is not assignable to type '(x?: string | undefined) => void'.ts(2769)
index.d.ts(2187, 9): The expected type comes from property 'onChange' which is declared here on type 'IntrinsicAttributes & Omit<Omit<Pick<DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>, "key" | keyof InputHTMLAttributes<...>> & { ...; } & Input, never> & Partial<...>, "theme"> & { ...; } & { ...; }'
index.d.ts(2187, 9): The expected type comes from property 'onChange' which is declared here on type 'IntrinsicAttributes & Omit<Omit<Pick<DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>, "key" | keyof InputHTMLAttributes<...>> & { ...; } & Input, never> & Partial<...>, "theme"> & { ...; } & { ...; }'

更新于onChange?: (x? : string ) =&gt; void,

【问题讨论】:

    标签: reactjs typescript styled-components


    【解决方案1】:

    错误的症结在于:Type 'undefined' is not assignable to type 'ChangeEvent&lt;HTMLInputElement&gt;'. 也就是说,在您的Input 类型中,您指定onChange 的参数可能是undefined(因为可选的?)或@ 987654326@ 而不是 ChangeEvent&lt;HTMLInputElement&gt;,但您随后尝试将处理程序分配给仅接受 ChangeEvent&lt;HTMLInputElement&gt; 而不是三种可能性中的任何一种的 onChange

    如果我了解您使用字符串与事件类型以及解包到 e.target.value 所完成的工作,您应该通过将两个 onChange 行更改为这样来获得所需的结果:

    onChange?: (x: string | ChangeEvent<HTMLInputElement>) => void,
    

    还有这个:

    onChange={(e: string | ChangeEvent<HTMLInputElement>):void => { onChange((typeof e === 'string') ? e : e.target.value)} }
    

    这里我完全取消了undefined 分支,假设在没有任何参数的情况下不会调用该方法。然后,我安排处理程序通过不变地传递string 来接受它。

    【讨论】:

    • 感谢您的回复。但我对onChange={(e: string | ChangeEvent&lt;HTMLInputElement&gt;):void =&gt; { onChange((typeof e === 'string') ? e : e.target.value)} } 感到困惑,我认为 e 总是等于`ChangeEvent` 而不是字符串,不是吗?
    • 发生错误的onChange处理程序被传递给InputWrapperDefault,它又是Input类型的styled.div版本,其中onChange被定义为string | ChangeEvent&lt;HTMLInputElement&gt;。参数类型传递给您的处理程序必须处理的内容。 (看起来您已经对代码进行了一些编辑,但我指的是上面的原始代码。)
    • 对不起,我弄错了。 @kdau
    猜你喜欢
    • 2021-04-21
    • 2021-05-25
    • 2022-07-29
    • 2021-01-26
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 2021-10-14
    • 2020-09-08
    相关资源
    最近更新 更多