Skip to content

No error for anonymous import that doesn't exist #28032

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

Closed
fatcerberus opened this issue Oct 21, 2018 · 5 comments
Closed

No error for anonymous import that doesn't exist #28032

fatcerberus opened this issue Oct 21, 2018 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@fatcerberus
Copy link

No compile-time error is produced for import 'nonexistent_filename'. An error is produced for nonexistent named imports (e.g. import X from './notExist.js'), but for anonymous imports (when simply importing for side effects) no error is produced if the file doesn't exist, the import is emitted as-is, and the code fails at runtime.

TypeScript Version: 3.2.0-dev.20181020

Search Terms: anonymous import no error

Code

// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.
import foo from './doesnt-exist-1.js';
import './doesnt-exist-2.js';

Expected behavior:
2 errors should be produced:

  • Cannot find module ./doesnt-exist-1.js
  • Cannot find module ./doesnt-exist-2.js

Actual behavior:
Only 1 error is produced:

  • Cannot find module ./doesnt-exist-1.js

Playground Link:
http://www.typescriptlang.org/play/#src=import%20foo%20from%20'.%2Fdoesnt-exist-1.js'%3B%0D%0Aimport%20'.%2Fdoesnt-exist-2.js'%3B%0D%0A

Related Issues:
Couldn't find any.

@ajafff
Copy link
Contributor

ajafff commented Oct 21, 2018

Duplicate of #27289

@fatcerberus
Copy link
Author

Figures. I never pick the right search terms...

@fatcerberus
Copy link
Author

I checked into the other issues, it seems this is intentional behavior for backwards compatibility. I definitely understand that; that said, I’m a big refactor-fiend and this bit me after deleting a side-effect-only file from the project (I sometimes use these for polyfills), so if there could be a flag to optionally make this an error, it would be great to have. 😃

@ajafff
Copy link
Contributor

ajafff commented Oct 21, 2018

I'm planning to write a lint rule to do this check as long as the compiler doesn't make it an error: fimbullinter/wotan#426

@typescript-bot
Copy link
Collaborator

This issue has been marked as a duplicate and has seen no activity in the last day. It has been closed for automatic house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants