Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!