Setting Up TypeScript and Eslint With Eglot

Using TypeScript with eglot in Emacs is fiddly to set up. I also prefer using eslint as a plugin to typescript-language-server so no other setup is required. Together, this makes for an extremely portable setup with minimal fuss which is the whole point of the language server protocol to begin with.

Install the typescript-language-server:

npm install -g typescript-language-server

Install the eslint plugin for typescript-language-server:

npm install typescript-eslint-language-service -D

Add the plugin to your config’s compilerOptions:

{...
  "compilerOptions": {
     "plugins": [{
      "name": "typescript-eslint-language-service"
    }]
    ...
  }
 }

In init.el, tell eglot how to locate your tsconfig.json (otherwise your TypeScript project configuration won’t be picked up and eslint won’t work):

;; I'm not sure why this is needed, but it throws an error if I remove it
(cl-defmethod project-root ((project (head eglot-project)))
  (cdr project))

(defun my-project-try-tsconfig-json (dir)
  (when-let* ((found (locate-dominating-file dir "tsconfig.json")))
    (cons 'eglot-project found)))

(add-hook 'project-find-functions
          'my-project-try-tsconfig-json nil nil)

(add-to-list 'eglot-server-programs
             '((typescript-mode) "typescript-language-server" "--stdio"))

See also: