Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 774
COLEAN Avatar asked Sep 19 '25 15:09

COLEAN


1 Answers

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.

like image 191
T.J. Crowder Avatar answered Sep 21 '25 03:09

T.J. Crowder