COLEAN
COLEAN

Reputation: 695

How to export and import at the same time in typescript

I want to import the model's interface and at the same time export the model interface from that file.

I wrote the following code to import the model's interface and use it in that file and export that interface externally.

// api/service/post.interface.ts
export import { Post } from '../../model/interface/post.interface.ts; 
// -> An import declaration cannot have modifiers.

type PostPayload = Partial<Post>;

// api/service/post_create.ts
import { Post } from './post.interface'; 
// -> this path has no exported member 'Post'

const a = (title: Post['title']) => {
  ...
}

What did I make a mistake?

Upvotes: 4

Views: 7355

Answers (2)

DAG
DAG

Reputation: 6994

You "reexport" a name like this:

export { Post } from "../../model/interface/post.interface.ts";

Or even the default export of a module like this:

export { default } from "../../model/interface/somedefault.ts";

And even the default export under a new name:

export { default as Other name } from "../../model/interface/somedefault.ts";

But there is no syntax to import and export at the same time.

Upvotes: 2

T.J. Crowder
T.J. Crowder

Reputation: 1075377

You can't both import something to use locally and export it in a single declaration, they have to be separate ones:

import { Post } from '../../model/interface/post.interface.ts;
export { Post }; 

Although it's possible to re-export something in a single statement, like this:

export { Post } from '../../model/interface/post.interface.ts;

...it doesn't create a local binding you can use. It's just a re-export, not an import.

Upvotes: 8

Related Questions