No estoy seguro de cómo articular lo que estoy tratando de hacer, por lo que es difícil encontrar respuestas específicas, pero aquí va.
Si tengo archivos simples con objetos individuales como
typeOne.ts
export const types = { name: 'blah' ... } typeTwo.ts
export const types = { name: 'blehh' ... }Tengo otra clase en alguna parte que tomará el nombre de cuál extraer y realizar funciones
public getTypes(typeName: string) { return {typeName from typeOne ... typeTwo ...} // how to import here? }Y entonces puedo llamarlo básicamente
const theTypes = this.getTypes('typeOne');o
const theTypes = this.getTypes('typeTwo'); Entonces, ¿eso es lo que estoy tratando de lograr para que la función getTypes sea genérica y no necesite definir cada uno individualmente?
Gracias
Alias sus importaciones
import { types as typeOne } from "./typeOne" import { types as typeTwo } from "./typeTwo" import { Type } from "./Type" const allTypes = { typeOne, typeTwo } const getTypes = (typeName: keyof typeof allTypes): Type => allTypes[typeName] // Type.ts export interface Type { name: string } Otro enfoque de la función getTypes podría verse como...
const getTypes = (typeName: string): Type => { switch (typeName) { case "typeOne" : return typeOne case "typeTwo" : return typeTwo default : throw new Error(`Unknown type "${typeName}"`) } }