News for hackers. Stuff that matters.
Re:The TypeScript compiler is now implemented internally wit... by redox99 on Wednesday November 02, 2022 @03:35PM (#33441717)
I absolutely hate how with Typescript and ES Modules, if you have a file

utils/foo.ts

you have to import it as

import Foo from "utils/foo.js"

Even though there is no .js file on disk, and you might be running ts-node or whatever that doesn't build a .js file.

Importing a file that "doesn't exist" is so counterintuitive.

In addition all code breaks because you have to change all your imports, and /index.ts or /index.js won't work either.

Re:The TypeScript compiler is now implemented internally wit... by bigyikes on Wednesday November 02, 2022 @03:42PM (#33441838)
Every TypeScript project I have worked on either:

1) enforces no extension, e.g. “utils/foo”, or

2) allows TS extensions, e.g. “utils/foo.ts”

I have never imported a TS file using a JS extension. Maybe your woes could be fixed with a configuration change?

Re:The TypeScript compiler is now implemented internally wit... by cypress66 on Wednesday November 02, 2022 @04:03PM (#33442157)
And that's how it should work imo. But if you enable esm (which you might need in the future because of packages being esm only) you can't use those, only .js.

That's because typescript developers are dead set that they don't want to transpile the imports, they just want to copy paste them into the resulting file when running tsc.

Re:The TypeScript compiler is now implemented internally wit... by conaclos on Wednesday November 02, 2022 @05:19PM (#33443051)
This could change with an ongoing work to allow ts extension [1].

[1] https://github.com/microsoft/TypeScript/issues/37582

Re:The TypeScript compiler is now implemented internally wit... by iainmerrick on Wednesday November 02, 2022 @07:20PM (#33444230)
That doesn’t look like an issue that’s going to be resolved any time soon. Lots of comments which read like people digging in their heels to preserve the current behavior.
Re:The TypeScript compiler is now implemented internally wit... by conaclos on Thursday November 03, 2022 @05:38AM (#33448844)
It was in the 4.9 iteration plan [1], and is now in the 5.0 iteration plan [2]. They also talk of this in a design meeting [3] and a member of TS team seems committed to it.

[1] https://github.com/microsoft/TypeScript/issues/50457

[2] https://github.com/microsoft/TypeScript/issues/51362

[3] https://github.com/microsoft/TypeScript/issues/51302

Re:The TypeScript compiler is now implemented internally wit... by iainmerrick on Thursday November 03, 2022 @09:27AM (#33450619)
Well, I would love to see it, so I hope I’m wrong and you’re right!
Re:The TypeScript compiler is now implemented internally wit... by bakkoting on Thursday November 03, 2022 @02:23PM (#33455211)
You'll only be able to use that if you are using the non-default `--noEmit` configuration, which requires the use of some other tool to strip types. Even after that change, using `tsc` to strip types will still force you to write `.js` in your imports.
Re:The TypeScript compiler is now implemented internally wit... by sebdufbeau on Wednesday November 02, 2022 @03:53PM (#33442000)
From my understanding, this is a fairly new predicament, for projects that target ESM (type module in package.json) instead of the default CJS
Re:The TypeScript compiler is now implemented internally wit... by dickfickling on Wednesday November 02, 2022 @03:51PM (#33441981)
have you worked on Typescript projects using ES modules? What you’ve described is the status quo for CommonJS modules, but doesn’t work when you switch to ESM (afaik, at least)
Re:The TypeScript compiler is now implemented internally wit... by Chyzwar on Wednesday November 02, 2022 @05:47PM (#33443342)
No, you were using non-standard ESM modules (compiled to CommonJS defined by babel) Typescript recently added support for ESM compatible with node.js see "module": "node16"[1][2]

The Whole ESM saga is clusterfuck, not much better than python 2 -> 3 migration. Large node.js codebases have no viable path to migrate, and most tools still cannot support ESM properly[3]. Stuff is already breaking because prolific library authors are switching to ESM.

As someone that maintain large part of TS/JS tooling in my day job, I absolutely despise decisions made by node.js module team. My side projects are now in Elixir and zig because these communities care about DX.

  [1] https://nodejs.org/api/esm.html#differences-between-es-modules-and-commonjs
  [2] https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html
  [3] https://github.com/facebook/jest/issues/9430
Re:The TypeScript compiler is now implemented internally wit... by redox99 on Wednesday November 02, 2022 @06:08PM (#33443590)
Yeah it's pretty ugly. This whole thing is a prime example of those cases in which maintainers for mostly arbitrary reasons decide on something and then absolutely ignore all the massive negative feedback they get for this.

They'll cite some nebulous technical reasons of why it has to be this way, but if you offer a PR that actually solves the issue that the community complains about, they'll reject it.

In this case they decided that tsc won't transpile imports. They just did and it's "policy" and it can't be changed. It doesn't matter if it is awful for compatibility, developer experience, etc. It's just the policy. Issues will be closed. And no, even an optional flag to transpile imports is off the table, even if you write the PR for it.

There are many many issues opened related to this in github, but to give an example

https://github.com/microsoft/TypeScript/issues/16577

Re:The TypeScript compiler is now implemented internally wit... by Chyzwar on Wednesday November 02, 2022 @06:26PM (#33443759)
It is complicated but most user anger should be directed to node.js module group[1]. TS is forced to follow node.js standard.

[1] https://github.com/nodejs/modules/issues/323

Re:The TypeScript compiler is now implemented internally wit... by iainmerrick on Wednesday November 02, 2022 @07:12PM (#33444168)
Hmm, what is it that Node is doing that’s so bad? I don’t understand why that issue is a big problem.

This explanation in the comments makes sense to me:

Transpilers can add the ability to add extensions at compile time, so a specifier like './file' can be rewritten to './file.js' during compilation along with whatever else is getting converted by the transpiler.

It seems sensible for Node to expect fully-qualified imports, just like a browser would. And (to me) it seems sensible that in a language like TypeScript you should be able to import “foo.ts” and it have it transpiled to the correct filename.

Now, that does not work in TS because they adamantly refuse to modify any of the emitted JavaScript code at all, with no clear explanation except that it’s long-standing policy. Instead they expect you to import “foo.js” in TypeScript, even though that file doesn’t exist until after compilation. That’s a problem, and it seems like it’s caused by the TS team, not Node.

Re:The TypeScript compiler is now implemented internally wit... by Chyzwar on Wednesday November 02, 2022 @07:52PM (#33444575)
Browsers never required extensions, see https://unpkg.com/ You can load scripts in browser just fine without extension.

> Now, that does not work in TS because they adamantly refuse to modify any of the emitted JavaScript code at all, with no clear explanation except that it’s long-standing policy. Instead they expect you to import “foo.js” in TypeScript, even though that file doesn’t exist until after compilation. That’s a problem, and it seems like it’s caused by the TS team, not Node.

I think they should provide a better explainer. But node.js resolution algorithms is already incredibly complicated, and adding path rewriting to typescript is not going to make it better. There are things like dynamic import and third part libraries. Typescript would need to either analyze whole of project node_modules or bundle custom runtime resolver like webpack breaking compact with deno and friends.

Imagine situation:

  import lib from 'somelib/subpath'
How TS would know that some lib have extension in subpath and it need to add/remove js ext? https://nodejs.org/api/packages.html#extensions-in-subpaths What if typescript is running in deno/bum or wasm?

> Hmm, what is it that Node is doing that’s so bad? I don’t understand why that issue is a big problem.

My conclusion is that successful projects without BDFL are prone to corporate takeovers. You have people that working in corporations without writing code and want to make political career as "core" team member of project.

Re:The TypeScript compiler is now implemented internally wit... by zarzavat on Thursday November 03, 2022 @05:14AM (#33448675)
TS can already perform complicated refactorings, such as renaming all imports in a project when you rename a file in vscode. So they definitely have all the information they need already.

Node requiring the .js is more understandable, as that actually affects runtime performance: if the name isn’t fully qualified in source then node needs to stat() more paths during resolution.

> What if typescript is running in deno/bum or wasm?

Every TS project already needs a tsconfig.json to specify what permutation of module system configurations it is using.

Re:The TypeScript compiler is now implemented internally wit... by Chyzwar on Thursday November 03, 2022 @10:25AM (#33451484)
> Node requiring the .js is more understandable, as that actually affects runtime performance: if the name isn’t fully qualified in source then node needs to stat() more paths during resolution.

Performance is affected only during startup by maybe few milliseconds. This is a major breaking change to module resolution made by node.js. Why TS should paper over change that was made by node.js.

> TS can already perform complicated refactorings, such as renaming all imports in a project when you rename a file in vscode. So they definitely have all the information they need already.

Some things are supported by tsserver not typescript compiler. Fixing this in TS is only addressing the problem partially and is shifting the problem into tools developers.

Re:The TypeScript compiler is now implemented internally wit... by WorldMaker on Thursday November 03, 2022 @04:13PM (#33456819)
The complication here is that Node in part decided on needing fully-qualified imports not just to better align with the browser, but also to use multiple file extensions in a complex signaling process (ETA: which the browser uses mime-types and metadata like type="module" for rather than file extensions): the file extension could be any of .js, .cjs, and .mjs, and then depending on package.json and some other metadata, Node may load the various files in all sorts of different ways.

Typescript decided that they don't always know what output file extension you may need because Node made that logic way too complex and they don't want to just reimplement their own buggy version of Node's loader mechanics but "backwards" in a terrible "guess the file extension" game, so they went with the easiest option which was "user now has to tell us the file extension".

Even if Typescript didn't have a policy to reduce the number of modifications it emits, it's still Node's fault that the file extensions now have three options with an extremely complex dance between them and getting a "guess the file extension" game right would be an extended PITA.

Re:The TypeScript compiler is now implemented internally wit... by iainmerrick on Thursday November 03, 2022 @07:29PM (#33459425)
If I’m in a TypeScript file and I import “foo.ts”, why can’t TSC just rewrite that to whatever filename TSC will emit when it compiles foo.ts?

If you’re just using TSC to typecheck and not emitting code (which is what most people actually do in practice), it’s even easier -- just let me import “foo.ts” if that’s the name of a file that exists. The popular bundlers can all handle that just fine.

Re:The TypeScript compiler is now implemented internally wit... by WorldMaker on Friday November 04, 2022 @03:01AM (#33463071)
Because TSC isn't your bundler. It's job is different from a bundler. A bundler runs under the assumption that everything you import it has to find and handle. Typescript only needs to find a definition for an import.

Typescript may have a complete view of a single project, in which case yes, it should know what file type it is emitting in that project, but then it has to track "in project" imports differently from "out of project" imports and needs two different behaviors for those.

All of that gets further complicated by incremental builds and multi-project references and multi-project references with incremental builds.

Which isn't to say that it isn't technically solvable, and maybe "two behaviors" is an alright developer experience even if it would confuse so many new users, it's just that there are a lot of obvious complications in the face of it.

It's also not like they haven't been trying to work on it. Other comments in this thread have pointed to at least one Typescript issue on it. At one point Previews supported a version of this but rather than using the file-type of the current package's emit it relied on the new paired TS file types: .ts => .js, .mts => .mjs, .cts => .cjs. If you imported a ".ts" it always assumed you were importing ".js" and if you imported a ".mts" it always assumed you were importing a ".mjs". There were a lot of complications even with that simple "one experience", but even that experience was terrible, fell down in complications with Node's loader and various bundlers, and had too many bugs. So it was pulled from Previews.

Re:The TypeScript compiler is now implemented internally wit... by iainmerrick on Friday November 04, 2022 @03:14AM (#33463124)
I realise it’s not trivial, but it’s really hard to believe it’s that difficult!

When I look at the multiple(!) issues on TS GitHub asking “please, can we just import .ts files with a .ts extension? That would make life a lot easier”, the comments from the developers pushing back on it aren’t about Node integration issues, they’re about the unshakable principle that TS must never rewrite syntactically valid JavaScript at all.

Make it work within a single project, at least, and leave external projects for later.

Edit to add:

There were a lot of complications even with that simple "one experience", but even that experience was terrible, fell down in complications with Node's loader and various bundlers, and had too many bugs. So it was pulled from Previews.

Do you have a link handy for that discussion? I’d be interested to read it.

Re:The TypeScript compiler is now implemented internally wit... by WorldMaker on Sunday November 06, 2022 @10:30PM (#33500773)
> they’re about the unshakable principle that TS must never rewrite syntactically valid JavaScript at all.

Which is starting to be a very useful principle of Typescript. Typescript knows that today it is not your bundler and tries to leave almost all rewriting to your bundler. That leaves bundlers able to strip types without even needing a direct dependency on Typescript. This is why tools like swc and esbuild written entirely in other languages now type strip as well.

This is also why the Typescript team has been a proponent for a proposal to add type stripping (or something like it) to the entire web platform. (There's a Stage 1 proposal in TC-39's process.)

You may not find that useful, but there's a growing ecosystem around "Typescript is just for types, not also for deeper transpilation". It is not just TS developer being "high and mighty" in the face of things you think they could make the developer experience easier on.

Re:The TypeScript compiler is now implemented internally wit... by iainmerrick on Monday November 07, 2022 @11:21AM (#33507862)
I think we’re violently agreeing?

If TypeScript isn’t the bundler, it shouldn’t complain (as it currently does) when you import a “.ts” file. It shouldn’t care at all!

Re:The TypeScript compiler is now implemented internally wit... by tobyhinloopen on Thursday November 03, 2022 @03:57AM (#33448214)
Elixir + Phoenix is so much better :) we have some apps running in production, some are 6 years old.
Re:The TypeScript compiler is now implemented internally wit... by tobyhinloopen on Thursday November 03, 2022 @03:54AM (#33448196)
No, it’s new, to comply with new stuff from nodejs.

You can likely change it with a config, but do note that importing using .js will be the new standard way of doing things and by changing it through configuration you’re chosing to not follow the new standard.

It’s a complete mess IMO. Every project uses a different way to handle modules and there’s a lot of rough edges.

Re:The TypeScript compiler is now implemented internally wit... by lewisl9029 on Wednesday November 02, 2022 @04:37PM (#33442605)
Agree completely. It also makes interop between Node and Deno more painful. But there is hope on the horizon. :)

See https://github.com/microsoft/TypeScript/issues/37582 which is referenced in the 4.9 Iteration Plan as "Support .ts as a Module Specifier for Bundler/Loader Scenarios": https://github.com/microsoft/TypeScript/issues/50457

Re:The TypeScript compiler is now implemented internally wit... by codefined on Wednesday November 02, 2022 @05:10PM (#33442964)
Actually looks to be tabled for TypeScript 5.0, release date of 'March 14th' next year.
Re:The TypeScript compiler is now implemented internally wit... by cypress66 on Wednesday November 02, 2022 @05:28PM (#33443153)
But that doesn't seem like it would fix the typical flow, there still would be no transpiling of imports. So yes you could use .ts in ts-node, but you would have to use .js in tsc. Which is pretty awful (you want your code to work with both).
Re:The TypeScript compiler is now implemented internally wit... by cush on Wednesday November 02, 2022 @05:08PM (#33442933)
Where are you seeing this? The PR that this thread is about doesn't have this quirk. Maybe your setup has some issue...?
Re:The TypeScript compiler is now implemented internally wit... by matt_kantor on Thursday November 03, 2022 @08:13AM (#33449841)
Their complaints are unrelated to this specific PR.

See https://www.typescriptlang.org/docs/handbook/esm-node.html for details about how import paths work in CommonJS vs ESM. In both cases the import path you write in your source code is the same import path that is used in the emitted JavaScript. What's different is that NodeJS's ESM implementation doesn't allow extensionless import paths (but its CommonJS implementation does).

Re:The TypeScript compiler is now implemented internally wit... by kuramitropolis on Wednesday November 02, 2022 @05:34PM (#33443222)
A thousand times this. It's not only the dumbest thing I've seen a programming language do, it's also dumbest thing I've seen in the JS ecosystem. Ended up having to implement an AST-based post-processor to fix packages before publishing them.