-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
Help WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
Description
π Search Terms
"commonjs", "import", "type", "module"
β Viability Checklist
- 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, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β Suggestion
Proposal: Allow importing pure types from .mts
files from a CommonJS file.
The current behavior generates an unnecessary / incorrect error since no require
will be emitted.
Please note, future versions of Node will support requiring an ESM module if a there is not a top level await
. See:
π Motivating Example
src/code.cts
import type { EntityId, Entity, User } from './models.mjs';
export function userName(user: User): string {
return user.name;
}
src/models.mts
export type EntityId = string;
export interface Entity {
id: EntityId;
}
export interface User extends Entity {
name: string;
}
π» Use Cases
- What do you want to use this for?
In mixed code bases (especially as they transition from CommonJS to ESM) it is often necessary export the types from a module that are used in CommonJS files. - What shortcomings exist with current approaches?
All shared types must be declared in CommonJS files or inferred usingAwaited
. - What workarounds are you using in the meantime?
Declaring shared types in CommonJS files.
Metadata
Metadata
Assignees
Labels
Help WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases