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

128
Visualizações
Typescript object with default function typing in React

I wonder how this code written in JavaScript

const stuff = useCallback(() => { 
    function first() { 
        return "firstStaff"; 
    }
    function major() { 
        return "majorStaff";
    }
    
    major.first = first;
    
    return major;
})();

Can be written with correct types in TypeScript, so it has the right hints for stuff() and stuff.first()

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

We can define a type by combining multiple types like this to achieve required

type Stuff = { first: () => string } & (() => string);

const stuff: Stuff = useCallback(() => {
...    
})();

  stuff.first();

  stuff();
about 4 years ago · Juan Pablo Isaza Relatório

0

If you are interested in function static property typing you can use this example:

import { useCallback } from 'react'

interface Major {
  (): string
  first: () => string
}

const stuff = useCallback((): Major => {
  function first() {
    return "firstStaff";
  }
  function major() {
    return "majorStaff";
  }

  major.first = first;

  return major;
}, [])();

Playground

Please see this question/answer if you want to know more about typing static function properties

about 4 years ago · Juan Pablo Isaza Relatório

0

useCallback need an array of dependency as the 2nd argument then it can be self infer

import { useCallback } from "react";

export default function App() {
  const stuff = useCallback(() => {
    function first() {
      return "firstStaff";
    }
    function major() {
      return "majorStaff";
    }

    major.first = first;

    return major;
  }, []);

  return (
  ...
  );
}

Working Example:

Edit summer-night-zx2bm

about 4 years ago · Juan Pablo Isaza Relatório
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