2015-12-02 14:23:39 +00:00
|
|
|
|
#+TITLE: TypeScript layer
|
2015-06-10 16:44:30 +00:00
|
|
|
|
|
2019-05-07 08:53:56 +00:00
|
|
|
|
#+TAGS: general|js|layer|multi-paradigm|programming
|
2019-05-05 17:26:40 +00:00
|
|
|
|
|
2015-06-10 16:44:30 +00:00
|
|
|
|
[[file:img/TypeScript.png]]
|
|
|
|
|
|
2018-09-19 03:54:47 +00:00
|
|
|
|
* Table of Contents :TOC_4_gh:noexport:
|
2017-05-22 14:16:12 +00:00
|
|
|
|
- [[#description][Description]]
|
2017-10-22 22:18:33 +00:00
|
|
|
|
- [[#features][Features:]]
|
2017-05-22 14:16:12 +00:00
|
|
|
|
- [[#install][Install]]
|
|
|
|
|
- [[#pre-requisites][Pre-requisites]]
|
2018-05-08 04:10:13 +00:00
|
|
|
|
- [[#choosing-a-backend][Choosing a backend]]
|
|
|
|
|
- [[#backends][Backends]]
|
|
|
|
|
- [[#tide][Tide]]
|
|
|
|
|
- [[#notes][Notes]]
|
|
|
|
|
- [[#language-server-protocol][Language Server Protocol]]
|
2017-05-22 14:16:12 +00:00
|
|
|
|
- [[#key-bindings][Key bindings]]
|
|
|
|
|
- [[#typescript-major-mode][Typescript Major Mode]]
|
|
|
|
|
- [[#reference-major-mode][Reference Major Mode]]
|
2015-06-10 16:44:30 +00:00
|
|
|
|
|
|
|
|
|
* Description
|
2016-01-25 16:19:39 +00:00
|
|
|
|
This layer adds support for TypeScript and TSX editing.
|
2015-06-10 16:44:30 +00:00
|
|
|
|
|
2017-10-22 22:18:33 +00:00
|
|
|
|
** Features:
|
2018-05-08 04:10:13 +00:00
|
|
|
|
- Multiple backends support: Tide and LSP
|
2016-02-17 12:34:30 +00:00
|
|
|
|
- Eldoc-mode
|
2016-06-10 04:09:42 +00:00
|
|
|
|
- Documentation at point
|
2016-01-25 16:19:39 +00:00
|
|
|
|
- Auto complete
|
2016-02-17 12:34:30 +00:00
|
|
|
|
- Flycheck with linter
|
2016-01-25 16:19:39 +00:00
|
|
|
|
- Jump to definition, Jump to type definition
|
2016-02-17 12:34:30 +00:00
|
|
|
|
- Find occurrences (Imenu-mode)
|
2016-01-25 16:19:39 +00:00
|
|
|
|
- Rename symbol
|
|
|
|
|
- tsx mode
|
2016-02-02 22:49:37 +00:00
|
|
|
|
- formatting
|
2016-02-18 21:04:10 +00:00
|
|
|
|
- TypeScript playground integration
|
2015-06-10 16:44:30 +00:00
|
|
|
|
|
|
|
|
|
* Install
|
2016-01-06 05:21:55 +00:00
|
|
|
|
To use this configuration layer, add it to your =~/.spacemacs=. You will need to
|
|
|
|
|
add =typescript= to the existing =dotspacemacs-configuration-layers= list in this
|
|
|
|
|
file.
|
2015-06-10 16:44:30 +00:00
|
|
|
|
|
2016-01-25 16:19:39 +00:00
|
|
|
|
#+BEGIN_SRC emacs-lisp
|
2018-09-19 03:54:47 +00:00
|
|
|
|
(setq-default dotspacemacs-configuration-layers '(typescript))
|
2016-01-25 16:19:39 +00:00
|
|
|
|
#+END_SRC
|
2015-06-10 16:44:30 +00:00
|
|
|
|
|
2016-02-02 22:49:37 +00:00
|
|
|
|
If you need formatting on save:
|
|
|
|
|
|
|
|
|
|
#+BEGIN_SRC emacs-lisp
|
2018-09-19 03:54:47 +00:00
|
|
|
|
(setq-default dotspacemacs-configuration-layers '(
|
|
|
|
|
(typescript :variables
|
|
|
|
|
typescript-fmt-on-save t)))
|
2015-06-10 16:44:30 +00:00
|
|
|
|
#+END_SRC
|
|
|
|
|
|
2016-05-02 09:11:16 +00:00
|
|
|
|
You can choose formatting tool:
|
|
|
|
|
|
|
|
|
|
#+BEGIN_SRC emacs-lisp
|
2018-09-19 03:54:47 +00:00
|
|
|
|
(setq-default dotspacemacs-configuration-layers '(
|
|
|
|
|
(typescript :variables
|
|
|
|
|
typescript-fmt-tool 'typescript-formatter)))
|
2016-05-02 09:11:16 +00:00
|
|
|
|
#+END_SRC
|
|
|
|
|
|
2018-09-19 03:54:47 +00:00
|
|
|
|
Default is ='tide=.
|
2016-05-02 09:11:16 +00:00
|
|
|
|
|
2018-05-08 04:10:13 +00:00
|
|
|
|
** Pre-requisites
|
|
|
|
|
You will need =node.js v0.12.0= or greater.
|
2016-02-02 22:49:37 +00:00
|
|
|
|
|
2018-05-08 04:10:13 +00:00
|
|
|
|
If you want linting run:
|
2016-01-25 16:19:39 +00:00
|
|
|
|
|
2018-05-08 04:10:13 +00:00
|
|
|
|
#+BEGIN_SRC shell
|
2018-09-19 03:54:47 +00:00
|
|
|
|
npm install -g typescript tslint
|
2018-05-08 04:10:13 +00:00
|
|
|
|
#+END_SRC
|
2016-01-25 16:19:39 +00:00
|
|
|
|
|
2018-05-08 04:10:13 +00:00
|
|
|
|
If you want to use typescript-formatter for formatting run:
|
2016-02-18 21:04:10 +00:00
|
|
|
|
|
2018-05-08 04:10:13 +00:00
|
|
|
|
#+BEGIN_SRC shell
|
2018-09-19 03:54:47 +00:00
|
|
|
|
npm install -g typescript-formatter
|
2018-05-08 04:10:13 +00:00
|
|
|
|
#+END_SRC
|
2016-01-25 16:19:39 +00:00
|
|
|
|
|
2018-05-08 04:10:13 +00:00
|
|
|
|
For best results, make sure that the =auto-completion= (company) and =html=
|
|
|
|
|
layers are enabled.
|
2017-07-14 20:58:30 +00:00
|
|
|
|
|
2018-05-08 04:10:13 +00:00
|
|
|
|
** Choosing a backend
|
|
|
|
|
To choose a default backend set the layer variable =typescript-backend=:
|
|
|
|
|
|
|
|
|
|
#+BEGIN_SRC elisp
|
2018-09-19 03:54:47 +00:00
|
|
|
|
(setq-default dotspacemacs-configuration-layers '(
|
|
|
|
|
(typescript :variables typescript-backend 'tide)))
|
2018-05-08 04:10:13 +00:00
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
|
|
Backend can be chosen on a per project basis using directory local variables
|
|
|
|
|
(files named =.dir-locals.el= at the root of a project), an example to use the
|
|
|
|
|
=lsp= backend:
|
|
|
|
|
|
|
|
|
|
#+BEGIN_SRC elisp
|
2018-09-19 03:54:47 +00:00
|
|
|
|
;;; Directory Local Variables
|
|
|
|
|
;;; For more information see (info "(emacs) Directory Variables")
|
|
|
|
|
((typescript-mode (typescript-backend . lsp)))
|
2018-05-08 04:10:13 +00:00
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
|
|
*Note:* you can easily add a directory local variable with ~SPC f v d~.
|
|
|
|
|
|
|
|
|
|
* Backends
|
|
|
|
|
** Tide
|
2017-07-14 20:58:30 +00:00
|
|
|
|
Tide comes with an embedded Typescript server, it is recommended to use the
|
|
|
|
|
server intalled by =npm= instead. To do so set the variable
|
|
|
|
|
=tide-tsserver-executable= to the path of the =tsserver= executable.
|
|
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
|
|
#+BEGIN_SRC emacs-lisp
|
2018-09-19 03:54:47 +00:00
|
|
|
|
(setq-default dotspacemacs-configuration-layers '(
|
|
|
|
|
(typescript :variables
|
|
|
|
|
tide-tsserver-executable "/usr/local/bin/tsserver")))
|
2017-07-14 20:58:30 +00:00
|
|
|
|
#+END_SRC
|
2016-01-25 16:19:39 +00:00
|
|
|
|
|
2018-05-08 04:10:13 +00:00
|
|
|
|
*** Notes
|
|
|
|
|
Make sure to add [[https://github.com/Microsoft/TypeScript/wiki/tsconfig.json][tsconfig.json]] in the project root folder.
|
|
|
|
|
|
|
|
|
|
tsserver mangles output sometimes [[https://github.com/Microsoft/TypeScript/issues/2758][issue - #2758]], which will result in json parse
|
|
|
|
|
error. Try node version 0.12.x if you get this error.
|
|
|
|
|
|
|
|
|
|
Send to playground requires browser.
|
|
|
|
|
|
2018-09-19 03:54:47 +00:00
|
|
|
|
Currently tsserver doesn’t pickup tsconfig.json file changes. You might need to
|
2018-05-08 04:10:13 +00:00
|
|
|
|
restart server after editing it.
|
|
|
|
|
|
|
|
|
|
** Language Server Protocol
|
2019-01-10 20:03:57 +00:00
|
|
|
|
You also need to install the Typescript Language Server.
|
|
|
|
|
Consult the installation command for the desired language server found at [[https://www.github.com/emacs-lsp/lsp-mode/][lsp-mode]] for instructions.
|
2018-05-08 04:10:13 +00:00
|
|
|
|
|
2015-06-10 16:44:30 +00:00
|
|
|
|
* Key bindings
|
2016-01-25 16:19:39 +00:00
|
|
|
|
** Typescript Major Mode
|
|
|
|
|
|
2018-12-05 03:03:03 +00:00
|
|
|
|
| Key binding | Description |
|
2016-02-18 21:04:10 +00:00
|
|
|
|
|-------------+--------------------------------------------------------------|
|
|
|
|
|
| ~SPC m =~ | reformat the buffer |
|
2018-07-03 21:31:41 +00:00
|
|
|
|
| ~SPC m E d~ | add =tslitn-disable-next-line= at point |
|
|
|
|
|
| ~SPC m E e~ | fix thing at point |
|
2016-02-18 21:04:10 +00:00
|
|
|
|
| ~SPC m g b~ | jump back |
|
2018-09-19 03:54:47 +00:00
|
|
|
|
| ~SPC m g g~ | jump to entity’s definition |
|
|
|
|
|
| ~SPC m g t~ | jump to entity’s type definition |
|
2016-02-18 21:04:10 +00:00
|
|
|
|
| ~SPC m g u~ | references |
|
|
|
|
|
| ~SPC m h h~ | documentation at point |
|
2018-10-13 23:09:18 +00:00
|
|
|
|
| ~SPC m r i~ | organize imports |
|
2016-02-18 21:04:10 +00:00
|
|
|
|
| ~SPC m r r~ | rename symbol |
|
2018-10-13 23:09:18 +00:00
|
|
|
|
| ~SPC m r f~ | rename file |
|
2016-02-18 21:04:10 +00:00
|
|
|
|
| ~SPC m s p~ | send selected region or current buffer to the web playground |
|
2018-05-02 15:42:14 +00:00
|
|
|
|
| ~SPC m s r~ | restart server |
|
2016-01-25 16:19:39 +00:00
|
|
|
|
|
2016-04-13 03:31:38 +00:00
|
|
|
|
** Reference Major Mode
|
2016-01-25 16:19:39 +00:00
|
|
|
|
|
2018-12-05 03:03:03 +00:00
|
|
|
|
| Key binding | Description |
|
2016-02-02 22:49:37 +00:00
|
|
|
|
|-------------+-------------------------|
|
|
|
|
|
| ~C-j~ | find previous reference |
|
|
|
|
|
| ~C-k~ | find next reference |
|
|
|
|
|
| ~C-l~ | goto reference |
|