ob-typescript icon indicating copy to clipboard operation
ob-typescript copied to clipboard

Emacs org-mode babel support for typescript

  • ob-typescript [[http://melpa.org/#/ob-typescript][http://melpa.org/packages/ob-typescript-badge.svg]]

This org-mode babel extension enables you to execute typescript code blocks.

** Install

*** Requirement You need to install node.js and typescript to use this extension.

*** MELPA With setting up [[http://melpa.org][MELPA]], use =M-x package-install ob-typescript= .

*** Manually Add ob-typescript.el to your =load-path= and require.

#+BEGIN_SRC emacs-lisp (add-to-list 'load-path "/path/to/ob-typescript.el") (require 'ob-typescript) #+END_SRC

** Configure

#+BEGIN_SRC emacs-lisp (org-babel-do-load-languages 'org-babel-load-languages '((typescript . t) )) #+END_SRC

The default command used to run the typescript compiler is defined in [[help:org-babel-command:typescript][org-babel-command:typescript]]. You may configure this to do things like use a sandboxed version of the typescript compiler without having to install it globally

#+begin_src emacs-lisp (setq org-babel-command:typescript "npx -p typescript -- tsc") #+end_src

** Examples

*** Execute with node.js

#+BEGIN_SRC typescript :results output :var x="foo" :var y='("bar" "baz") module Greeting { export class Hello { constructor(private text : string) { } say() :void{ console.log(${this.text}, ${x}, ${y}); } } }

var hello : Greeting.Hello = new Greeting.Hello("Hello, World!"); hello.say(); #+END_src

#+RESULTS: : Hello, World!, foo, bar,baz

*** Transpile

You can see transpile results by specifying ":wrap src js" header argument.

#+BEGIN_SRC typescript :wrap "src js :results output" module Greeting { export class Hello { constructor(private text : string) { } say() :void{ console.log(this.text); } } }

var hello : Greeting.Hello = new Greeting.Hello("Hello, World!"); hello.say(); #+END_SRC

#+RESULTS: #+begin_src js :results output var Greeting; (function (Greeting) { var Hello = /** @class */ (function () { function Hello(text) { this.text = text; } Hello.prototype.say = function () { console.log(this.text); }; return Hello; }()); Greeting.Hello = Hello; })(Greeting || (Greeting = {})); var hello = new Greeting.Hello("Hello, World!"); hello.say(); #+end_src