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

172
Visualizações
typescript use object as the type definition

Let's say I have

clients/
  index.ts
  client1.ts
  client2.ts
myApp.ts

// client1.ts
export default class Client1 {}
// client2.ts
export default class Client2 {}

// clients/index.ts
import Client1 from './client1';
import Client2 from './client2';
export { client1, client2 };

Now, I want to use all of clients in myApp and set the type to be based on this, i.e.

// myApp.ts

import * as clients from './clients';

type Clients = {
 [key in keyof typeof clients]: clients[key];
}

const allClients: Clients = {
  client1: new clients.Client1(),
  client2: new clients.Client2(),
}

The code above is not valid, clients[key]; throws an error about Cannot use namespace 'clients' as a type. but I basically want to be able to import all of the clients, and use that object as the type definition for all my clients. Is this possible?

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

0

Why not just create the interface manually? I don't think you're going to be able to create it on the fly like that. And you probably want your types to be static at runtime anyway. They'll be much easier to reason about.

Just define an interface with the clients listed either myApp or where you export them.

import * as clients from './clients';

interface Clients = {
   client1: clients.client1,
   client2: clients.client2,
}

const allClients: Clients = {
  client1: new clients.Client1(),
  client2: new clients.Client2(),
}
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