Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
TypeScript how to type hint a function

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?

Extension:

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
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda