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

288
Views
Typescript dynamically declare named export in d.ts

I made a library of components for VueJs in TypeScript and I want to be able to use them globally in my Vue instance (with Vue.use(library)) or one by one (with named import in Vue compoenent)

It works fine, but I have some issues with TypeScript for declaring all the components in index.d.ts file when I want to do it dynamically (because there are many components and I don't want to do it manually)

So this is working :

import { Component1, Component2 } from 'my-library';

declare module 'my-library' {
  declare const lib: PluginFunction<any>;
  export default lib;

  export { Component1, Component2 } from 'my-library';
}

But I'm looking for a way to do this for all the components without importing and exporting them one by one.

I tried something like this :

...
export * from 'my-library'
...

or

import * as components from 'my-library';

declare module 'my-library' {
  declare const lib: PluginFunction<any>;
  export default lib;

  Object.entries(components).forEach(([componentName, component]) => {
    export { component as componentName };
    // or
    // export { component };
  })
}

But it doesn't work, I got TS errors when I do import { Component1 } from 'my-library':

"Cannot resolve symbol 'Component1' TS2614: Module '"my-library"' has no exported member 'Component1'. Did you mean to use 'import Component1 from "my-library"' instead?"

ps: everything works fine if I use a //@ts-ignore before the import.

Any idea?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

My guess is this is because TS requires things at build time to do its "typeschecking" stuff. In other words, TS is more restrictive than JS and this is one of the prime examples where you lose flexibility because it wants to have definitions at build time rather than runtime.

So in short, you can't do this. The only thing you can do it is export everything in an key'ed object, but that means when you import it in another file you have to take the whole thing and can't take part of it.

File.vue

<CustomNamedThing/>

<script>
import Foo from './yourfile.ts'
components: {
  CustomNamedThing: Foo.CustomNamedThing
}, ...


</script>
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!