-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Remove "An import path cannot end with a '.ts' extension" check (rewriting out-of-scope) #38149
Comments
This would enable me to update my JS-extension-adding TSC transformer such that it accepts a compiler option for "target file extension" and automatically rewrites any import targeting |
Yeah, this would be great for Deno's development experience and opens the possibilty to write code for both Deno and Node 👏 |
Note that the linked question ( #27481 ) had dozens of upvotes. I'd guess that as deno grows the number of people that get false-positives for deno imports will also grow. |
We can use |
Since there is more discussion going on here I link to my issue as a copy. |
I came here to the same. It's strange / unpleasant that TypeScript doesn't allow an |
@matthew-dean to be clear, nothing requires extensions except node’s native ESM, because extensions don’t even exist in the browser. Browsers use URLs, and there’s no reason your asset URLs have to 1:1 match filenames on a filesystem. |
@ljharb Browsers require an exact path match, and TypeScript outputs |
You’re exactly right - but I’ve yet to encounter a system where one of those things isn’t pre-existing or trivial to add. |
Stripping extensions from files on disk makes them not able to load automatically in the proper editor or have proper syntax highlighting auto-detect. Server side script is impossible for IPFS, S3, or any other static file hosting solution. Module loaders are what we are trying to avoid here, they are bulky and add complexity to the project and debugging. All of my projects these days are hosted via static file hosts, and I have gone down the runtime module loader path and it is not pretty. |
I think Typescript imports should have (be forced?) a |
Just to note something, one reason that this isn't as trivial to support as it seems is because of the behaviour with dynamic import. In order to support dynamic import correctly, a transform would have to be created that correctly identifies package local files from global files (e.g. in Deno, even if you compile to While this isn't super super difficult, without a lot of decisions on when to apply rules, or providing a large amount of configuration so that people can create these rules themselves it becomes difficult to reason about such behaviour. Having said this I'd still generally be supportive of allowing The TSC team has mentioned in the past it doesn't want TypeScript to be an end-to-end build tool, but it could still offer more for processing TypeScript specific things like this itself. Interestingly if TypeScript simply supported |
@Jamesernator absolutely... of course, the tsc could have an option to either ignore or place an That said, if you are importing |
Search Terms
"An import path cannot end with a '.ts' extension"
Relevant: #27481 (question, but this one is a proposal)
Suggestion
I would like check "An import path cannot end with a '.ts' extension" to be removed. Note that that any extension transformations are out of scope for this -- having
.ts
as-is in compiled code is acceptable.Use Cases
I would like to always use explicit extension in imports as it is consistent with ESM
import
behavior in both browser and node, and also teaches the right things to others. In the same way LESS uses.less
when importing, and Webpack and some other cases analyze imports based on extensions.This aligns with TypeScript Design Goal 6: Align with current and future ECMAScript proposals and avoids Non-Goal 7 of behavior that is likely to surprise users (people familiar with imports but not TypeScript expect extensions).
I don't mind that extensions are not rewritten as I already have a babel step to add
.js
extension, and in some cases it needs to be.mjs
, which will probably be covered by #18442 anyways.This matches Non-Goal 4 by not expecting TypeScript to be an end-to-end pipeline (bundling or Babel rewrite will sort
.ts
).I know I can use
.js
but it is non-standard and very confusing to have in source, so for now I prefer not using extensions and rewriting to.js
post-compile.Examples
New
compiles to
Existing
compiles to
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: