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"
  (interactive)
  (let* (
         ($compile-options
          (substring
           (if current-prefix-arg
               (ido-completing-read
                "tsc options:"
                '(
                  "1 → --target ES2015 --alwaysStrict"
                  "2 → --target ES5 --alwaysStrict"
                  "3 → Ask"
                  "4 → None"
                  ))
             "1 → --target ES2015 --alwaysStrict"
             )
           4))
         ($outputb "*typescript compile output*")
         (resize-mini-windows nil)
         $fname
         ($prog-name "tsc")
         $cmd-str
         $options-string
         )
    (when (not (buffer-file-name)) (save-buffer))
    (when (buffer-modified-p) (save-buffer))
    (setq $fname (buffer-file-name))
    (setq $options-string
          (cond
           ((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. Open File Path Under Cursor
  2. Copy File Path of Current Buffer
  3. Run Current File
  4. Delete Current File
  5. Backup Current File
  6. Batch Convert Line Ending in Dired
  7. Open File in External App
Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

If you have a question, put $5 at patreon and message me.