Skip to content

Support importing types from a .mts file from within a CommonJS file.Β #58195

@Jason3S

Description

@Jason3S

πŸ” Search Terms

"commonjs", "import", "type", "module"

βœ… Viability Checklist

⭐ 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.

Current error:
image

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

  1. 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.
  2. What shortcomings exist with current approaches?
    All shared types must be declared in CommonJS files or inferred using Awaited.
  3. What workarounds are you using in the meantime?
    Declaring shared types in CommonJS files.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Help WantedYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions