Emacs: Compile TypeScript File ๐
Here's a emacs command that compiles the current TypeScript file.
(defvar xah-js-ts-compile-options "--target ES2021 --alwaysStrict" "Default options for TypeScript compiler. For example: โ--target ES2020 --alwaysStrictโ. Used by `xah-js-ts-compile-file'. URL `http://xahlee.info/emacs/emacs/elisp_compile_typescript.html' ")
(defun xah-js-ts-compile-file () "Compile the current typescript file. The file name must end in โ.tsโ or โ.tsxโ. If the file is modified, save it automatically before run. Depends on `xah-js-ts-compile-options'. URL `http://xahlee.info/emacs/emacs/elisp_compile_typescript.html' Created: 2020-12-13 Version: 2025-11-28" (interactive) (let ((resize-mini-windows nil) (xOptTemp (if current-prefix-arg (let ((completion-ignore-case t)) (completing-read "tsc options:" '( "None" "Ask" "--target ES5 --alwaysStrict" "--target ES2015 --alwaysStrict" "--target ES2016 --alwaysStrict" "--target ES2017 --alwaysStrict" "--target ES2018 --alwaysStrict" "--target ES2019 --alwaysStrict" "--target ES2020 --alwaysStrict" "--target ES2021 --alwaysStrict" "--target ES2022 --alwaysStrict" "--target ES2023 --alwaysStrict" "--target ES2024 --alwaysStrict" "--target ES2025 --alwaysStrict") nil t)) xah-js-ts-compile-options)) (xoutputb "*typescript compile output*") xfname xfExt xcmdStr xOptsStr) (when (not buffer-file-name) (user-error "Buffer is not file. Save it first.")) (when (buffer-modified-p) (save-buffer)) (setq xfname buffer-file-name) (setq xfExt (file-name-extension xfname)) (when (and (not (string-equal xfExt "ts")) (not (string-equal xfExt "tsx"))) (error "File name ใ%sใ must end in .ts or .tsx" (file-name-nondirectory xfname))) (setq xOptsStr (let ((x87873 (cond ((string-equal xOptTemp "None") "") ((string-equal xOptTemp "Ask") (read-string "tsc option:" xah-js-ts-compile-options)) (t xOptTemp)))) (if (string-equal xfExt "tsx") (concat x87873 " --jsx react ") x87873))) (setq xcmdStr (concat "tsc " (shell-quote-argument xfname) " " xOptsStr)) (message "Running ใ%sใ" xcmdStr) (shell-command xcmdStr xoutputb)))