Reputation: 49
I want to define few functions which will be treated as constants within a file and then export them in an array, but being new to typescript not sure how to do it.
For example I want how to declare Objs below. Objs will hold all the functions in an array.
interface Prop {
readonly var1: string;
readonly var2: string;
}
const Obj1 = (stage: string): Props => {
return {
var1: `a1-${stage}`,
var2: 'a2'
}
}
const Obj2 = (stage: string): Props => {
return {
var1: `b1-${stage}`,
var2: 'b2'
}
}
export const Objs (????????)
I want to use Objs in a different file like this
Objs.obj2("Test") or Objs.obj1("Test")
Upvotes: 0
Views: 274
Reputation: 3936
Export the objects using below syntax and use them in expected way in your code files.
export const Objs = {
Obj1,
Obj2
};
But I see you have also renamed to obj1 and obj2. Would suggest to have shorter and descriptive objects to be exported. So better to export as is or follow the another ans below.
Upvotes: 0
Reputation: 576
export const Objs = {obj1:Obj1, obj2:Obj2}
And in the file you are importing you can use as follows
import {Objs} from './myFile';
console.log(Objs.obj1("xyz"));
Upvotes: 1