Repository navigation
Remove "An import path cannot end with a '.ts' extension" check (rewriting out-of-scope) #38149
Description
Activity
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
.tsto target the configured extension (e.g.,.mjsor.js) and it would also allow the code to run ints-nodeor any other pure-TS environment (e.g., deno). This change would allow my transformer to be much more robust (at the moment it treats everything with a.in the path as "not eligible for transformation") by giving me something I can explicitly target that also makes sense.Reacted by Vitor L Cavalcanti, Daniel Ramos, Tim Reichen, Alex Young, Konstantin Nesterov, Jesús Leganés-Combarro, Pedro Paulo and Michael J. RyanDanielRamosAcosta commented
on May 11, 2020 More actionsYeah, this would be great for Deno's development experience and opens the possibilty to write code for both Deno and Node 👏
Reacted by Jonathan Wilbur, Alex Young, Ondřej Želazko, Vitor L Cavalcanti, A5, Nick, Carlos Campos, Alexander Tesfamichael, Jo, Adrián de la Rosa and 11 moreReacted by Brian Kim and NickNote 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.
xiaoxiangmoe commented
on May 23, 2020 ContributorMore actionsWe can use
import { identity } from './identity.util.ts'in deno, in parcel, in vite and in webpack.Reacted by Daniel Ramos, Vitor L Cavalcanti, Alex, Roy Ivy III, Noah Dunbar, Jo, Adrián de la Rosa and Oncelock- addedIn DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
on Jun 1, 2020 Since there is more discussion going on here I link to my issue as a copy.
There I suggested to have the suffix mandatory and a flag--noImplicitSuffix(default: false) that can be set for older projects to work as before.Reacted by Alex Young, Statek-zdejcina, Jean Lescure, Jo, Jacob Mischka, Josh Junon, Remy Rylan, Tobias Goulden Schultz, Pedro Paulo, Paweł Płoneczka and 1 moreI came here to the same. It's strange / unpleasant that TypeScript doesn't allow an
.tsextension for imports, especially since the import spec will require extensions in-browser (AFAIK?). I'm working on a transpilation language that uses.tsimports, and ideally, I want to pass imports as they are, and specify (in my language) that.jsor.tsmust be explicit. I can work around this, but I was searching today about how to turn this error off.Reacted by Ten Bitcomb, Josh Junon, Pedro Paulo, Rick, Oncelock, Michael J. Ryan, hlovdal and T.J. CrowderMatthew Dean (@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.
Reacted by Josh Junon, Tim Reichen, Pedro Paulo, Michael J. Ryan, hlovdal and PointotechJordan Harband (@ljharb) Browsers require an exact path match, and TypeScript outputs
.jsfiles when transpiling. So unless you have some build-time script that will strip extensions from files on disk, a server-side script (not available in all environments like IPFS or local filesystem) that transform requests, or you have a module loader running in the browser that transforms requests, loadingfruit/applewill not resolve to anything since onlyfruit/apple.tsandfruit/apple.jsexist.Reacted by Vitor L Cavalcanti, Ray Myers, Jesús Leganés-Combarro, Josh Junon, Michael J. Ryan, Pointotech and T.J. CrowderYou’re exactly right - but I’ve yet to encounter a system where one of those things isn’t pre-existing or trivial to add.
Reacted by Josh Junon, Tobias Goulden Schultz, hlovdal and T.J. CrowderReacted by Brian Kim, Ten Bitcomb and Jacob MischkaStripping 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.
Reacted by Tea Reggi and T.J. CrowderI think Typescript imports should have (be forced?) a
.tsextension, and when transpiled, a.jsis being set instead. It's just a change in a string...Reacted by Pauan, Tim Reichen, Jacob Mischka, Josh Junon, Tobias Goulden Schultz, Pedro Paulo, Elletra, Michael J. Ryan, Oncelock, Eser Ozvataf and 4 moreJust 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
.js, you still need to be able to import.tsfrom the web).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
.tsrather than requiring the extension to be the same as the output target. It's currently kinda painful for generating multiple builds as TypeScript doesn't support this sort've thing directly.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
.tsin the import, it would actually be possible to map the.tsOUTSIDE of TypeScript's compiler, via tools such as import maps or"imports"in Node.js. These sort've solutions would actually still haveimport foo from "./foo.ts";inside the.jsfiles, but they're actually fairly trivial to map away in a way that also works with dynamic import.Reacted by Jacob MischkaJames Browning (@Jamesernator) absolutely... of course, the tsc could have an option to either ignore or place an
(outdir)/_cache/http/host/path/to/*.jsas an output option... Or just ignore urls starting withhttp,httpsand//leaving it to other tools to deal with.That said, if you are importing
./foo.tsthen proceed to load and output to./foo.js, then it's not too much to ask that you update references to./foo.tsto./foo.js... and do similar for caching http(s) references. This would absolutely make the most sense and be the least friction in practice as options. Yeah, it's easier to dismiss it because node will assume.jsextension, but the browser loading doesn't support it either. If you want to support targeting a modern browser, then matching and including the extension from input to output makes more sense, not less.- addedFix AvailableA PR has been opened for this issueA PR has been opened for this issue
on Nov 28, 2022
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
.tsas-is in compiled code is acceptable.Use Cases
I would like to always use explicit extension in imports as it is consistent with ESM
importbehavior in both browser and node, and also teaches the right things to others. In the same way LESS uses.lesswhen 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
.jsextension, 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
.jsbut it is non-standard and very confusing to have in source, so for now I prefer not using extensions and rewriting to.jspost-compile.Examples
New
compiles to
Existing
compiles to
Checklist
My suggestion meets these guidelines: