Issue
Let's say I have a sort of library file in some package with a bunch of exports like this:
// conversation.ts
const hello = 'Hello';
const goodbye = 'Goodbye';
export {
hello,
goodbye,
};
And then lets say I have another library like this:
// questions.ts
const howAreYou = 'how are you?';
const whereDoYouLive = 'where do you live?';
export {
howAreYou,
whereDoYouLive,
};
What I really want to do is to make conversation.ts extend the questions.ts library.
I would like to be able to do something like this:
// conversation.ts
const hello = 'Hello';
const goodbye = 'Goodbye';
import * as questions from "./questions.ts";
export {
hello,
goodbye,
// This is not possible, but I wish it was
...questions,
};
That way you could publish or import the conversation library and have the questions library included.
import {
hello,
howAreYou,
} from "./conversation";
The closest I can think of this would be to either do
A. Import the other file with * as
// conversation.ts
const hello = 'Hello';
const goodbye = 'Goodbye';
import * as questions from "./questions.ts";
export {
hello,
goodbye,
questions,
};
or B. Import each export in the other file by hand
// conversation.ts
const hello = 'Hello';
const goodbye = 'Goodbye';
import { howAreYou, whereDoYouLive } from "./questions.ts";
export {
hello,
goodbye,
howAreYou,
whereDoYouLive,
};
A. doesn't work because I want all the imports to be on the same level.
B. doesn't work because I will have a ton of imports and I'd rather not do them all by hand like that.
Is there any way to concat or spread export objects together in TypeScript?
(This is a bit of a strange question so let me stress that I'm just asking if it's technically possible, not what I should do instead, or how I should organize my code.)
EDIT: As per the answer below this is possible in JavaScript and TypeScript with export * from "./questions.
Solution
You have yet to consider exporting directly:
export * from "./questions.ts";
Answered By - caTS
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.