用于此 React 表单字段挂钩的正确类型是什么?

What is the correct type to use for this React form field hook?

我有一个 React 钩子(使用打字稿),但如果我使用 any 作为初始值的类型,我只能让它工作。

我尝试了 HTMLInputElementReact.FormEvent(甚至 React.FormEvent<HTMLInputElement>)的组合以及使用 input type="text" 尝试将输入元素限制为 type: string.

这是钩子:

import { useCallback, useState } from "react";

export default function useField(initialValue: any) {
  const [value, setValue] = useState(initialValue);

  const handleUpdate = useCallback(
    ({ currentTarget: { type, checked, value } }) => {
      setValue(type === "checkbox" ? checked : value);
    },
    []
  );

  return [value, handleUpdate];
}

这就是使用它的地方:

import useField from "./hooks/useField";

const App = () => {
  const [firstName, setFirstName] = useField("");
  const [lastName, setLastName] = useField("");
  const [age, setAge] = useField("");

  return (
    <div className="App">
      <form>
        <input value={firstName} name="firstName" onChange={setFirstName} />
        <input value={lastName} name="lastName" onChange={setLastName} />
        <input value={age} name="age" onChange={setAge} />
      </form>
    </div>
  );
};

export default App;

它实际上按原样工作,使用 initialValue: any 但我觉得类型应该比这更具体。如果我确实将其更改为更具体的类型(例如字符串),则会出现以下两个错误。

(JSX attribute) React.InputHTMLAttributes<HTMLInputElement>.onChange?: ((event: React.ChangeEvent<HTMLInputElement>) => void) | undefined
Type 'string | (({ currentTarget: { type, checked, value } }: any) => void)' is not assignable to type '((event: ChangeEvent<HTMLInputElement>) => void) | undefined'.
  Type 'string' is not assignable to type '((event: ChangeEvent<HTMLInputElement>) => void) | undefined'.ts(2322)
index.d.ts(1977, 9): The expected type comes from property 'onChange' which is declared here on type 'DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>'

第二个是关于输入的onChange事件:

(JSX attribute) React.InputHTMLAttributes<HTMLInputElement>.onChange?: ((event: React.ChangeEvent<HTMLInputElement>) => void) | undefined
Type 'string | (({ currentTarget: { type, checked, value } }: any) => void)' is not assignable to type '((event: ChangeEvent<HTMLInputElement>) => void) | undefined'.
  Type 'string' is not assignable to type '((event: ChangeEvent<HTMLInputElement>) => void) | undefined'.ts(2322)
index.d.ts(1977, 9): The expected type comes from property 'onChange' which is declared here on type 'DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>'

今天是我使用 Typescript 的第一天,所以这可能是非常明显的事情!

如果您不指定 return 类型,TypeScript 将对其进行推断。我将从一个简单的例子开始:

function strNum() {
  return [1, 'a'];
}

这个return是(string | number)[]类型,即可以包含字符串或数字元素的数组。

useField return 是 string | (event: ChangeEvent<HTMLInputElement>) => void 的数组。这意味着数组的任何元素都可以是这些类型中的任何一种,并且它们彼此不兼容。

相反,您可以将 return 类型指定为元组。这是一个具有特定元素类型的固定长度的数组。

useField(initialValue: string): [string, (event: React.ChangeEvent<HTMLInputElement>) => void] {