Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export all interfaces/types from one file in TypeScript project

The direct interface to my library is captured by

index.d.ts

which is currently auto-generated from

index.ts

in my package.json file for my project, typings/types properties point to this index.d.ts file, which is to my understanding, how it should be. All good so far.

However, there are some more types that I would like to export from index.d.ts that are not currently expressed in index.d.ts.

For example I have some manually created types in

foo.d.ts
bar.d.ts

and I want these to be expressed in index.d.ts.

The only way I think that might work, is to import the foo.d.ts/bar.d.ts types into index.d.ts and then in turn export them from that file.

However, I am having trouble with that!

Here is what I tried:

// index.ts

import {IBar} from '../bar.d.ts'
import {IFoo} from '../foo.d.ts'

// ... do some stuff with index.ts yadda yadda

export type IBar = IBar; // doesn't seem to work
export interface IFoo = IFoo; // doesn't seem to work

is this possible? How do I do this?

like image 246
Alexander Mills Avatar asked Jun 16 '17 07:06

Alexander Mills


1 Answers

You can export them directly if you don't need to use them.

export * from '../foo/bar';

Or if you need to export only some of them you can use.

export { IFoo, IBar} from '../foo/bar';

Or if you want to rename them during export you can do:

export { IFoo as IOther, IBar as Dogs } from '../foo/bar';

You can read more about them in the MDN documentation here.

like image 94
toskv Avatar answered Sep 22 '22 06:09

toskv