Skip to content

How to import from the parent module? #20

@nicolo-ribaudo

Description

@nicolo-ribaudo

Module fragments can refer to each other, but it might also be useful to provide some syntax to allow importing the outer module. For example:

export function add(x, y) {
  return x + y;
}

export module test {
  import { add } from super; // 'super' to refer to the outer module
  
  assert(add(0, 2) === 2);
  assert(add(1, 2) === 3);
}

There is also the option of not reserving any specific syntax, and modules would have to write the explicit specifier of the outer module:

// ./math.js

export function add(x, y) {
  return x + y;
}

export module test {
  import { add } from "./math.js";
  
  assert(add(0, 2) === 2);
  assert(add(1, 2) === 3);
}

Note that if we provide some syntax to do it, it's more useful to refer to the top-most module rather than to its direct parent:

export const x = 1;

module M1 {
  export const y = 2;

  module M2 {
    import { y } from M1; // it's easy to import outer module declarations
    import { x } from ...; // how to import the top-level module?
  }
}

This use case was initially suggested at tc39/proposal-module-expressions#34.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions