Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jbrantly opened this issue Aug 7, 2015 · 4 comments
Closed
Labels
Question An issue which isn't directly actionable in code

Comments

@jbrantly
Copy link

jbrantly commented Aug 7, 2015

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.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 10, 2015

@jbrantly the current module resolution logic is a bit weird :D. if you are loafing module "helpers", the compiler will look for a file called "helpers.d.ts" in the current directory, then will keep walking up the directory tree searching for "helpers.d.ts"...

obviously this is not correct, hence #2338. @vladima is actively working on this now. so hopefully we have some relief soon.

but just to make sure i understood correctly, you want it to be an error to not use "./" or "../" with relative modules?

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Aug 10, 2015
@jbrantly
Copy link
Author

but just to make sure i understood correctly, you want it to be an error to not use "./" or "../" with relative modules?

In short, yes.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 10, 2015

good.. it should be soon :D

@mhegazy mhegazy closed this as completed Aug 10, 2015
@jbrantly
Copy link
Author

Thanks for confirmation of intended behavior 👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

2 participants