Skip to content

ES6 module import doesn't require relative identifier to load relative file #4214

@jbrantly

Description

@jbrantly

This could be by design but it just surprised me and I wanted to verify behavior.

With CommonJS, relative modules must start with "./" or "../", otherwise it's considered to be a top-level module.

As near as I can tell with ES6, module naming is "loader implementation specific". It would seem that TypeScript's implementation does not require "./" or "../" to reference a relative file. For example:

// dep.ts

export class Test {}

// app.ts

import { Test } from 'dep';

Is this intended? I'm not super familiar with prevailing ES6 module loading standards, but I put together a quick test using SystemJS. It also does not allow referencing a relative module without './' or '../' unless that module happens to be in the preconfigured "base" URL. In other words, the above example would probably work out of the box, but the following would not:

// components/helper.ts

export function someHelper();

// components/dep.ts

import * as helper from 'helper'; // this line would error at runtime in SystemJS, but not in TS

export class Test {}

// app.ts

import { Test } from 'components/dep';

My understanding is that work is being made in #2338 and things like #4154 which may help to alleviate issues like this since it would allow TypeScript to be aware of that "loader-implementation specific" part. In the meantime, is the current default correct? As an alternative, perhaps it would make sense for ES6 imports to not have this behavior if --module commonjs is set, since this clearly does not follow the CJS spec.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions