Open
Description
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 codeThis wouldn't change the runtime behavior of existing JavaScript codeThis could be implemented without emitting different JS based on the types of the expressionsThis 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)
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
harrysolovay commentedon Dec 3, 2020
I often want to export values which double as types. For instance.
It feels to me like this should be possible with default exports as well.
hopeless-programmer-online commentedon Apr 16, 2021
Any updates on this?
export * as X
cannot be imported whentype X
is exported #44896PolariTOON commentedon Mar 28, 2023
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 exportX
is not clearly marked as a type:And a workaround is to mark
X
as a type in theexport
declaration by using a named export:But that looks fairly indirect. Having
export default type X;
would also help making that more straightforward.AndreiSoroka commentedon Sep 9, 2023
was:
now:
seems like a bug, because I have 1 type in 1 file
switch Card form interface to type
export default interface
is allowed withverbatimModuleSyntax
enabled #57518movahhedi commentedon May 14, 2024
For those who were looking for "Type annotations for
export default
", follow #13626.@typescript-eslint/consistent-type-exports
and@typescript-eslint/consistent-type-imports
molindo/eslint-config-molindo#97