Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
Export all constants from files into index.ts and then export those constants as keys of an object?

I have a constant library that has many files such as the following structure:

#1 index.ts

#2 src/

  • contact.ts
    • const first = [];
    • const second = [];
    • const third = [];
  • location.ts
    • const first = '';
    • const second = '';
    • const third = '';

Is it possible to import and export all of the constants from each file into index.ts such that I can import into a project such as the following:

// Imports: Constants
import { constants } from '@jeff/constants-library';

console.log(constants.contact.first);
console.log(constants.contact.second);
console.log(constants.contact.third);

What is the fastest/most efficient way to dynamically export the constants from my library files so I can import the into my projects?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In your files like contact and location, you'll need to mark the const values you want as exports. Modules are designed for encapsulation, so dynamic export isn't really an option here. However, it's just a matter of adding the keyword export:

export const first = [];

After that, you can create a constants-library.ts or constants-library/index.ts which automatically exports and imports:

import * as foo from './foo';
import * as bar from './bar';

export const constants = { foo, bar };

At this point, assuming your @jeff path is set up, your code should work as expected:

import { constants } from '@jeff/constants-library';

console.log(constants.contact.first);

Note that this will include all of the exports in your files, since TypeScript can't tell which constants you want or didn't want--it only knows which exports you've listed. As an alternative, rather than exporting all of your consts individually, you could bundle them into a single export using object shorthand.

// in contact.ts and location.ts
export constants = { first, second, third };

// in constant-library.ts or constant-library/index.ts
import { constants as contact } from './contact';
import { constants as location } from './location';

export constants = { contact, location };
 
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda