TypeScript:如何使用泛型输入react-query useMutation onError?

Mis*_*hko 9 typescript reactjs typescript-generics react-hooks react-query

考虑一个自定义useSignUp钩子,该mutate()钩子函数需要一些字段(nameemail本例中):

export default function App() {
  const name = "David";
  const email = "david@gmail.com";
  const signupMutation = useSignUp();

  return (
    <button
      onClick={() => {
        signupMutation.mutate(
          { name, email },
          {
            onSuccess: (result) => {
              ...
            },
            onError: (result) => {
              // Wanted: result of type ErrorResult<"name" | "email">
            }
          }
        );
      }}
    >
      Sign Up
    </button>
  );
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我希望onError的结果是这种类型ErrorResponse<"name" | "email">。但是,我明白了ErrorResponse<string>

这是为什么?我如何指示 TypeScript 根据传递的数据推断特定的类型(即"name" | "email",不是string)?

这是我的输入方式useSignUp

type SuccessResponse = {
  userId: string;
};

type ErrorResponse<T> = {
  userId?: string;
  formErrors?: Array<{
    field: T;
    type: string;
  }>;
};

export const useSignUp = <T extends string>() => {
  return useMutation<SuccessResponse, ErrorResponse<T>, Record<T, string>>(
    (data) => {
      return new Promise((resolve, reject) => {
        // Some logic here to either return a success or an error response
        if (Math.random() > 0.5) {
          resolve({
            userId: "1234"
          });
        } else {
          reject({
            formErrors: [
              {
                field: "email", // I want TypeScript to complain if it's neither "name" nor "email"
                type: "ALREADY_EXISTS"
              }
            ]
          });
        }
      });
    }
  );
};
Run Code Online (Sandbox Code Playgroud)

Nea*_*arl 5

你已经声明了一个带有通用错误字段的钩子,如果你不提供参数,它默认为一个字符串:

export const useSignUp = <T extends string>() => {
  return useMutation<SuccessResponse, ErrorResponse<T>, Record<T, string>>(...)
}
Run Code Online (Sandbox Code Playgroud)

所以请确保覆盖它:

type Field = 'name' | 'email';

// ...

useSignUp<Field>();
Run Code Online (Sandbox Code Playgroud)

如果您想根据提交的值输入错误:

const mutatedValues = {
  name: "David",
  email: "david@gmail.com"
};
const signupMutation = useSignUp<keyof typeof mutatedValues>();
Run Code Online (Sandbox Code Playgroud)
onError: (result) => {
  result.formErrors?.forEach((e) => {
    const { field } = e; // field is 'name' | 'email'
  });
}
Run Code Online (Sandbox Code Playgroud)

Codesandbox 演示