Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export default type #41409

Open
4 of 5 tasks
hopeless-programmer-online opened this issue Nov 5, 2020 · 5 comments
Open
4 of 5 tasks

export default type #41409

hopeless-programmer-online opened this issue Nov 5, 2020 · 5 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@hopeless-programmer-online
Copy link

hopeless-programmer-online commented Nov 5, 2020

Search Terms

export default type

Suggestion

Allow export default for type.

Use Cases

It would be very nice to have export default type to match with other default exports such as classes and interfaces.

Right now it looks very inconsistent to be able to export anything as default and only types with named export:

export default class MyClass {}
export default interface MyInterface {}
export type MyType = {}

The only way to achieve this now is to use annoying workaround:

type X = number | string

export { X as default }

This may cause confusion for newbies as there are no clear reasons why classes and interfaces can be exported as default, but not the types.

Examples

// x.ts
export default type X = {}

// main.ts
import X from './x'

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

(not sure on the last)

@DanielRosenwasser DanielRosenwasser added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Nov 9, 2020
@harrysolovay
Copy link

I often want to export values which double as types. For instance.

export const Tag = Symbol.for("@my-app/entry.Tag");
export type Tag = typeof Tag;

It feels to me like this should be possible with default exports as well.

@hopeless-programmer-online
Copy link
Author

Any updates on this?

@PolariTOON
Copy link

TypeScript 5.0 (in #52203) introduced a new option called --verbatimModuleSyntax that makes the following code produce the following error: An 'export default' must reference a value when 'verbatimModuleSyntax' is enabled, but 'X' only refers to a type., because the default export X is not clearly marked as a type:

type X = number | string;
export default X;

And a workaround is to mark X as a type in the export declaration by using a named export:

type X = number | string;
export type {X as default}; // or export {type X as default};

But that looks fairly indirect. Having export default type X; would also help making that more straightforward.

@AndreiSoroka
Copy link

was:

type SomePayload = {
 data1: number;
 data2: string;
};
export default SomePayload;
import type SomePayload from './SomePayload.type'

now:

export type SomePayload = {
 data1: number;
 data2: string;
};
import type { SomePayload } from './SomePayload.type'

seems like a bug, because I have 1 type in 1 file

wisskirchenj added a commit to wisskirchenj/vue-playground that referenced this issue Oct 15, 2023
unfortunately not working with export default - see here: microsoft/TypeScript#41409
@movahhedi
Copy link

movahhedi commented May 14, 2024

For those who were looking for "Type annotations for export default", follow #13626.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants