如何读取带有多个箭头和泛型的 TypeScript 签名?

How to read a TypeScript signature with multiple arrows and generics?

我试图从 react-hook-forms 中理解这个 TypeScript 签名:

handleSubmit: <TSubmitFieldValues extends FieldValues = TFieldValues>(onValid: SubmitHandler<TSubmitFieldValues>, onInvalid?: SubmitErrorHandler<TFieldValues>) => (e?: React.BaseSyntheticEvent) => Promise<void>;

特别但不只是我想了解:

  1. 泛型中的等号是什么意思?

    <TSubmitFieldValues extends FieldValues = TFieldValues>

  2. 签名里怎么会有两个箭头?

    [removed for shortening] => (e?: React.BaseSyntheticEvent) => Promise<void>

如果有人能逐步解释签名,那就太好了,谢谢!

What does the equals sign in the generic type mean?

这是默认类型。

How can there be two arrows in the signature?

它是 returns 另一个函数的函数。例如,这个函数:

const myWeirdAdd = arg1 => arg2 => arg1+arg2;

我们可以这样调用吗:

myWeirdAdd(1)(2); // returns 3;

它的类型是:

type CurriedAdd = (arg1: number) => (arg2: number) => number;