-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add support for .cjs
and .mjs
input files
#39840
Add support for .cjs
and .mjs
input files
#39840
Conversation
92c08fd
to
cc3f921
Compare
Those examples seem to be about the resolver features, not about the file extensions. The resolver is equally "broken" for |
@jkrems you are partial right at present it is as broken as typescript is broken it self. I can not explain all that in a short comment or issue but this result is the same as we got befor 3 years :). |
I think this issue can finaly not get fixed in this way typescripts module system needs refactor much more then accepting .mjs and cjs as extension for js handling. |
I think you are right that a more fundamental fix may be desirable. The question is: If just letting people import So what is the path forward short term, if not a patch like this? People who want to write packages that contain (some) native ESM should stop using vscode to do it? They should use some community fork with these changes and override the project TS (which the ts language server might then pick up..?)? |
@jkrems there exists a other workaround without recode that enables that index.d.ts // interface other typescript types export import
// can also export {}
// that means simply any. myapp.mjs import some from 'some-other' myapp.mjs.d.ts // This is typescript syntax it can now import .ts .d.ts and .js types and rexport them
export * as types from './index.d.ts'
export = types The export = is correct typescript syntax it makes now myapp.mjs import able and it will show it with correct types if you want to even import it with specifier as you would do in nodejs you can create additional myapp.d.ts // This is typescript syntax it can now import .ts .d.ts and .js types and rexport them
export * as types from './index.d.ts'
export = types Final usageimport myapp from './myapp' //will lookup myapp.d.ts while nodejs will resolve myapp.mjs all works
import myapp from './myapp.mjs' //will lookup myapp.mjs.d.ts all works |
@jkrems and the path forward is clear drop typescripts extension .ts and replace it with solutions like assert calls and jsdoc typechecking 👍 and also a flow like Comment Syntax maybe contributed to JSDoc if that does not exist |
I'm not sure how eslint relates to autocomplete. The following currently works in vscode without any // b.js
export default class Foo { bar() {} }
// a.js
import Foo from './b.js';
new Foo(). // <suggests bar() as an available method> It doesn't work if |
That is incorrect, as that patch only hacks together a way for the TypeScript language server to recognise the This PR adds proper validation, import auto‑complete and enforces that As for the |
@ExE-Boss do not get me wrong it is a good thing but microsoft will not merge it we would need to maintain a vscode fork for that and i am not joking i am fighting with them since 5+ years. Your Solution is better then nothing. And it is more then i got done till today :) So i already cherry picked it internal. but we need to make it more easy useable as sayed overall the path forward is to create or fork the deprecated language-server project then integrate for example this typescript version and create a installer for that so that existing vscode users can simple install this as additional language server |
Just wanted to say thank you for doing this work @ExE-Boss 🙏🏻 |
@ExE-Boss You will be remembered as a god if you pull this off. |
If some one does want to test this https://github.com/stealify/typescript npm i -g vscode-typescript@https://github.com/stealify/typescript
# Getting global path
npm root -g Hit f1 on keyboard open user settings and set "typescript.tsdk": "{your_global_npm_path}/vscode-typescript/lib" restart is not needed. |
That is the intended behaviour. |
@frank-dspeed That’s because all engines (including Node.js) forbid loading native ES Modules without the file extension. // This will fail at runtime if `module-with-dts` doesn't exist in the same directory,
// even if `module-with-dts.js` (or `module-with-dts.mjs`) does:
import "./module-with-dts"; Note that this PR makes it so that |
@ExE-Boss that is not true with the extension part we got a new I will go ahead then now with rollup-language-server to solve all that compat issues once and for ever with and without typescript 👍 thanks for your effort. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're not going to add .cjs and .mjs extensions until we have a complete plan for supporting them.
A few years ago when I was working on early support for .vue files, I had pretty good luck with manually telling emacs to treat arbitrary extensions as Typescript. That might still work, although probably intra-file support won't be great. |
@sandersn for context, I'm interested in this support landing since it's required for VS Code to automatically update import paths with |
UPDATE* At present i expirence no down sides in the EDITOR with such Changes even not with my version that sets everything to .js |
The TypeScript team hasn't accepted the linked issue #27957. This makes it less likely that we'll review or accept this PR. Try to get the originating issue accepted. |
Fixed in #45884 (TypeScript 4.5) |
I got fed up with not having IntelliSense or type checking in
.cjs
and.mjs
files, so I’ve decided to fix this myself..cjs
files #38784