Emacs: Compile TypeScript File

By Xah Lee. Date: . Last updated: .

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

(defun xah-typescript-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

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial