reddit / emacs

Auto-import in Typescript

NaiveProcedure755 · 2022-11-03 12:33
\[SOLVED\] The issue was with the lsp server I used (it SHOULD BE ts-ls). Comment/Answer: [https://www.reddit.com/r/emacs/comments/yl1n24/comment/iv27avi/?utm\_source=share&utm\_medium=web2x&context=3](https://www.reddit.com/r/emacs/comments/yl1n24/comment/iv27avi/?utm_source=share&utm_medium=web2x&context=3) ​ Hi, r/emacs! I'm new to emacs and this subreddit, so sorry if I do something wrong. I currently have to use VSCode because I'm dealing with typescript and react projects on the daily bases. That's the only thing stopping me from switch to emacs. I managed to setup ts suport with typescript mode and lsp, but I can't setup auto-imports. I guess it should be configured in LSP but I really can't find it. My config: [https://github.com/ToxyFlog1627/emacs](https://github.com/ToxyFlog1627/emacs). LSP: [https://github.com/ToxyFlog1627/emacs/blob/master/Emacs.org#lsp](https://github.com/ToxyFlog1627/emacs/blob/master/Emacs.org#lsp) Any help would be really appreciated!
KaranasToll · 2022-11-03 13:47
Have you tried having cursor over thing you want to be auto inported and calling M-x `lsp-execute-code-action`? It should give you to option to import.
[deleted] · 2022-11-03 16:20
[deleted]
NaiveProcedure755 · 2022-11-03 17:22
Hi, Yes, I've tried. It simply removed it, presumably because it though it was unused or something. Maybe I'm missing some setting?
NaiveProcedure755 · 2022-11-03 17:23
Hi, I've heard that it's slow and that lsp is better. Does it provide auto-imports? If so I will try it
agentlemanwrote · 2022-11-03 17:51
It does
NaiveProcedure755 · 2022-11-03 17:54
Cool, could you, if you have config of tide on github or elsewhere, post the link? Also, tide doesn't work with lsp, right? Thanks!
dasgurks · 2022-11-03 18:41
Can't second that. For me tide is still far superior to the "generic" LSP modes. Edit: and it nicely does auto imports.
emoarmy · 2022-11-03 23:44
I've never been able to get tide working for me; meanwhile, as long as I chose the right LSP server for TS/JS it's worked wonderfully for me.
NaiveProcedure755 · 2022-11-04 07:47
Hi! Could you say which server do you use for lsp? And also do you have auto-imports? Thanks!
hyrulz · 2022-11-04 11:50
How do I actually perform the auto-import? I must be blind.
NaiveProcedure755 · 2022-11-04 12:42
\+1 I also was unable to find it.
[deleted] · 2022-11-04 14:47
[deleted]
emoarmy · 2022-11-04 16:53
Yeah, totally. I use ~~jsts~~[ts-ls](https://emacs-lsp.github.io/lsp-mode/page/lsp-typescript/), the recommended one. I remember in the past the [sourcegraph](https://emacs-lsp.github.io/lsp-mode/page/lsp-typescript-javascript/) variant was really easy to install and was useless for me.
NaiveProcedure755 · 2022-11-04 17:09
Though I was using the same server I was unable to do auto-imports (M-x `lsp-execute-code-action` just deleted the class I wanted it to import, presumably because it though it was unused). Do they work for you?
emoarmy · 2022-11-04 18:46
So, I just went through this again to validated things and my link that says `jsts`* actually links to `ts-ls`. `ts-ls` is the language server that will autoimport for me. So if you run `M-x lsp-install-server RET ts-ls RET`, or `npm i -g typescript-language-server; npm i -g typescript`, it should preferentially use `ts-ls` over `jsts-ls` and autoimports should work automagically, or they do for me. For reference my lsp config is pretty [minimal](https://github.com/justinbarclay/.emacs.d#lsp-mode). *In fact if I run `jsts-ls` I get a message in my `*warnings*` buffer saying it is deprecated. I tried running with it anyways and `jsts-ls` also refused to autoimport for me. I wish they would remove `jsts-ls` as an install option =/
NaiveProcedure755 · 2022-11-04 19:03
THANKS <3!!! I'm not sure which part of config does that but it works now! I can finally switch from VSCode to Emacs and become a better man )))
emoarmy · 2022-11-04 19:06
Sorry for misleading you. I am happy that you got it working now 😍
agentlemanwrote · 2022-11-05 20:52
Here's what I use, I use `use-package` - if you don't use that, you can try pulling in what they show you on the tide github page: ```elisp (use-package tide :preface (defun setup-tide-mode () (tide-setup) (flycheck-mode +1) (setq flycheck-check-syntax-automatically '(save mode-enabled)) (eldoc-mode +1) (tide-hl-identifier-mode +1) (company-mode +1)) :mode ("\\.ts" . typescript-mode) :hook ((typescript-mode . setup-tide-mode) (ng2-ts-mode . setup-tide-mode)) :bind (("C-c t f" . tide-fix) ("C-c t o" . tide-format) ("C-c t r" . tide-rename-symbol)) :config (setq company-tooltip-align-annotations t) (setq typescript-indent-level 2) (setq tide-format-options '( :insertSpaceAfterFunctionKeywordForAnonymousFunctions t :placeOpenBraceOnNewLineForFunctions nil :indentSize 2 :tabSize 2 :placeOpenBraceOnNewLineForFunctions nil :placeOpenBraceOnNewLineForControlBlocks nil))) ```
agentlemanwrote · 2022-11-05 20:54
I'm using it with `(flycheck-mode +1)`, when flycheck runs (for me it's when I save a file), it shows an error for the non existing module/class/etc. I can move the cursor over it and do `M-x tide-fix`, and tide will offer suggestions including an import (or multiple imports if it can't differentiate).
NaiveProcedure755 · 2022-11-14 17:22
Sorry for not responsding, I stopped checking the post since I've found the solution. You're late, but thanks nonetheless for taking time to help me!