Emacs: Compile Current TypeScript File

By Xah Lee. Date: . Last updated: .

Here's a emacs command that compiles the current TypeScript file.

(defun xah-ts-compile-file (@prefix-arg)
  "Compile the current file.
The file name must end in “.ts” or “.tsx”.
If the file is modified or not saved, save it automatically before run.

URL `http://ergoemacs.org/emacs/elisp_compile_typescript.html'
Version 2018-03-01"
  (let* (
           (if current-prefix-arg
                "tsc options:"
                  "1 → --target ES2015 --alwaysStrict"
                  "2 → --target ES5 --alwaysStrict"
                  "3 → Ask"
                  "4 → None"
             "1 → --target ES2015 --alwaysStrict"
         ($outputb "*typescript compile output*")
         (resize-mini-windows nil)
         ($prog-name "tsc")
    (when (not (buffer-file-name)) (save-buffer))
    (when (buffer-modified-p) (save-buffer))
    (setq $fname (buffer-file-name))
    (setq $options-string
           ((string-equal $compile-options "None") "")
           ((string-equal $compile-options "Ask")
            (read-string "tsc option:" "--target ES5 --alwaysStrict"))
           (t $compile-options)))
    (setq $cmd-str (concat $prog-name " " (shell-quote-argument $fname) " " $options-string))
    (message "Runing %s" $cmd-str)
    (shell-command $cmd-str $outputb )))

See also: Emacs: Run Current File

Current File Commands

  1. Emacs: Open File Path Under Cursor
  2. Emacs: Copy File Path of Current Buffer
  3. Emacs: Run Current File
  4. Emacs: Delete Current File
  5. Emacs: Backup Current File
  6. Emacs: Batch Convert Line Ending in Dired
  7. Emacs: Open File in External App
Like my tutorial? Put $5 at patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

Ask me question on patreon