I would like to type hint my function in TypeScript as below:
// would like to remove this type
type FN<TArgs, TReturn> = (args: TArgs) => TReturn;
interface Args {
itemWeight: number;
quantity: number;
}
const calcTotalWeight: FN<Args, number> = ({ itemWeight, quantity }) => itemWeight * quantity;
Is there any built-in generic that provides this utility? Currently, I have created one as above, but I feel as if this could be a very common use case. For example, react has FC.
I do note that this type requires your function to be unary, but that's the whole point here. It's common to have a unary function, with the single argument an object so that we can have "named" parameters.
Does anyone know if this exists, or thoughts on whether this is a good idea?
Does anyone know how to make the second argument of the generic optional so that it is inferred if not provided? For example:
const calcTotalWeight: FN<Args> = ({ itemWeight, quantity }) => itemWeight * quantity;
// typeof calcTotalWeight === FN<Args, number> , or
// typeof calcTotalWeight === (Args) => number