Daros911
Daros911

Reputation: 435

Exporting typescript type definitions

In my project I'd like to have one file with all the typescript definitions like so:

type_defs.tsx:

interface T_oauth_obj {
    oauth_callback: string,
    oauth_consumer_key: string,
    oauth_nonce: string,
    oauth_signature_method: string,
    oauth_timestamp: string,
    oauth_version: number,
    oauth_signature: string,
}

interface IAuthRequests {
    getRequestToken: () => string
    Authorize(): void
    getAccessToken(): void  
}
...

The problem is that when i try to export the typedefs like so:

export {IAuthRequests, T_oauth_obj}

I get the error:

Re-exporting a type when the '--isolatedModules'...

As I've found in the net there can be only the default export for a typedef file that means that i have to prepare one file per type definition which is absolutely an absurd!

In my opinion the constraint not allowing to have multiple, non-default exports for typedefs is totally nonsense and making the coding redundant and hard to re-use. Having one file with all the typedefs can help having more tidy and debug-able code.

P.S

Is there a way to achieve the goal I'm talking about?

Upvotes: 0

Views: 1110

Answers (2)

Alex Wayne
Alex Wayne

Reputation: 187242

Your exported types can either be named, or each file can have one default type export. The export keyword simply does not allow for exporting multiple values.

So, since it only works for one value or type at a time, this won't work because {IAuthRequests, T_oauth_obj} is not a valid single type.

You could do a default export of a whole type, but this is probably not what you want:

export default { req: IAuthRequests, oauth: T_oauth_obj }

Or you could do:

export interface IAuthRequests { ... }
export interface T_oauth_obj { ... }

If you want one export keyword to export multiple named types, then I have no other help than to say it just doesn't do that. To answer why you'll probably have to ask the typescript maintainers. The answer is likely to be a deliberate design choice to more closely match how export works is plain JS.

Upvotes: 1

Adam Orłowski
Adam Orłowski

Reputation: 4484

Try like this:

export interface T_oauth_obj {
    oauth_callback: string,
    oauth_consumer_key: string,
    oauth_nonce: string,
    oauth_signature_method: string,
    oauth_timestamp: string,
    oauth_version: number,
    oauth_signature: string,
}

export interface IAuthRequests {
    getRequestToken: () => string
    Authorize(): void
    getAccessToken(): void  
}

and then import:

import { T_oauth_obj, IAuthRequests } from "your.file"

Upvotes: 1

Related Questions