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: Originally I asked if this was possible in TypeScript/ES6 and as suggested below, this is possible in JavaScript with export * from "./questions
, but it does not appear possible to use export *
without as
in TypeScript. (Just tested with the latest create-react-app
5.0.1
in stackblitz - demo)
CodePudding user response:
You have yet to consider exporting directly:
export * from "./questions.ts";
CodePudding user response:
What about using export default
?
export default {
hello,
goodbye,
...questions,
};