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

179
Visualizações
TypeScript singleton generated with a factory function looses typings

I have a sugary javascript SDK where the library is instantiated at any given point at runtime, but from then onwards is a singleton. As in, repeat calls to the default of the file return the same instance.

My problem is that my typings are lost between the definition of the library and the instantiation.

See this code:

const Factory = (args?: Record<string, unknown>) => {

  return {
    action: {
        method: (code: 'SOME'|'CODES') => console.log('Doing something with', code)
    }
  }
}

let instance

export default (args?: Record<string, unknown>) => {

  if (!instance)
    instance = Factory(args)

  return instance

}

If I instantiate the Factory directly, I'm furnished with typings correctly. However, the exported interface for the file is attempting to ensure the instance always returns a singleton. In that process we loose typings.

How should I type instance here, in order to retain the typings?

Constraints:

  • I don't want to have to re-type everything in an interface that loosely matches the definition
  • The initial instance can be instantiated by the user at any point during runtime (cannot be assumed to be created on bootstrap)
  • Shouldn't use classes due to the potential complexity of the nested methods that are returned.
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use the ReturnType utility type to get the inferred type of the instance that Factory returns, then use that in the type of instance and the return type of the default export. See LibInstanceType below:

const Factory = (args?: Record<string, unknown>) => {

  return {
    action: {
        method: (code: 'SOME'|'CODES') => console.log('Doing something with', code)
    }
  }
};

type LibInstanceType = ReturnType<typeof Factory>;

let instance: LibInstanceType | undefined;

export default (args?: Record<string, unknown>): LibInstanceType => {

  if (!instance)
    instance = Factory(args);

  return instance;

};

Playground link

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