Skip to content

Instantly share code, notes, and snippets.

@mjkrause
Last active March 27, 2020 17:18
Show Gist options
  • Save mjkrause/666550ee0074161f0e2403a5ccf7f8c7 to your computer and use it in GitHub Desktop.
Save mjkrause/666550ee0074161f0e2403a5ccf7f8c7 to your computer and use it in GitHub Desktop.
Setting up typescript

Using Typescript

We need Node.js (and NPM for convenience) - see below on how to install it. First run sudo apt update (e.g., on Ubuntu).

Next create a new directory, and in it run npm init, followed by npm -g typescript to install TypeScript globally. Create a file script.ts in the project root, and add the line <script src="script.js" defer></script> to index.html (e.g., to head). (use html:5 in VS Code to set up a skeleton in a new index file).

lite-server

Next install

npm install --save-dev lite-server

and add "start": "lite-server" to "scripts" in package.json. Then run npm start, which should open a server on port 3000 (default).

Next run tsc --init, which create tsconfig.json. Finally, run your TypeScript main file in watch-mode by tsc script.ts --watch, and let the process run. Now each time you save script.ts it should automatically be compiled.

nvm (Node Version Manager)

It is convenient to use Node JS (i.e., install) in combination with the version manager as it makes testing code on different versions of Node JS easy. Fetch the install script from here and run

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

Source the shell resource file (e.g., . .bashrc if bash is your preferred shell) or close and re-open the terminal and verify that nvm --version returns the version in URL above. If so, nvm is installed. In case of problem consult the trouble-shooting section in the link.

Set the version of Node JS

Using nvm we can now install first Node JS by

nvm install node
nvm install 10.16.3
nvm use 10.16.3

Next, we execute nvm list to see what other versions are available. For instance, we can make one version the default version by nvm install <version>, followed by nvm alias default <version>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment