Emacs operates on the principle of keybindings and modes. Keybindings are combinations of keys that perform specific actions, such as opening a file (C - x C - f
where C
stands for the Ctrl
key). Modes in Emacs are configurations that tailor the editor’s behavior for different types of files. For example, there are text modes, programming modes, and more.
TypeScript adds types to JavaScript. This means you can define the data types of variables, function parameters, and return values. For instance:
// Variable with a type annotation
let message: string = "Hello, TypeScript!";
// Function with type annotations
function add(a: number, b: number): number {
return a + b;
}
When using TypeScript in Emacs, you need to understand how Emacs can be configured to support these type - related features.
npm install -g typescript
typescript - mode
and tide
(TypeScript Interactive Development Environment). You can use the Emacs package manager (M - x package - install
) to install them.;; Add this to your .emacs or init.el file
(require 'package)
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(unless package-archive-contents
(package-refresh-contents))
;; Install typescript-mode and tide
(package-install 'typescript-mode)
(package-install 'tide)
Add the following configuration to your .emacs
or init.el
file to enable tide
for TypeScript files:
(defun setup-tide-mode ()
(interactive)
(tide-setup)
(flycheck-mode +1)
(setq flycheck-check-syntax-automatically '(save mode-enabled))
(eldoc-mode +1)
(tide-hl-identifier-mode +1)
;; company is an autocompletion framework
(company-mode +1))
;; aligns annotation to the right hand side
(setq company-tooltip-align-annotations t)
;; formats the buffer before saving
(add-hook 'before-save-hook 'tide-format-before-save)
(add-hook 'typescript-mode-hook #'setup-tide-mode)
C - x C - f
and then navigate to the .ts
file you want to edit.typescript - mode
will automatically provide syntax highlighting, making the code more readable.You can use the tsc
command in the Emacs shell (M - x shell
) to compile TypeScript files. For example, to compile a file named app.ts
to JavaScript:
tsc app.ts
If you want to compile all TypeScript files in a project, you can use the tsc
command without specifying a file name if you have a tsconfig.json
file in your project root.
With tide
and company - mode
enabled, you’ll get autocompletion suggestions as you type. For example, if you have a variable of type string
and you start typing a string method, you’ll see suggestions like toUpperCase
, toLowerCase
, etc.
tsconfig.json
A tsconfig.json
file is used to configure the TypeScript compiler options for your project. Here is a basic example:
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*.ts"]
}
In Emacs, having a tsconfig.json
file in your project root helps the tide
mode to provide more accurate type checking and autocompletion.
Use the tide - goto - definition
(M - .
) keybinding to jump to the definition of a variable, function, or class. You can use M - ,
to jump back to your previous location.
You can define your own keybindings in Emacs to make your TypeScript development more efficient. For example, you can bind a key combination to compile the current TypeScript file:
(global-set-key (kbd "C - c C - t")
(lambda ()
(interactive)
(shell-command (concat "tsc " (buffer-file-name)))))
Flycheck is a powerful error - checking tool in Emacs. With tide
and Flycheck enabled, you’ll see errors and warnings highlighted in your TypeScript code as you type. This helps you catch and fix issues early in the development process.
Using TypeScript in Emacs can be a highly productive experience once you understand the fundamental concepts and set up the necessary configurations. By following the usage methods, common practices, and best practices outlined in this blog post, you can write, compile, and debug TypeScript code more efficiently. Emacs’ extensibility allows you to customize your development environment to suit your specific needs, making it a great choice for TypeScript developers.