Some examples of using Typescript, for a Scottish Tech Army talk 'Introduction to Typescript'
Initial setup:
- Make sure you have NodeJS installed on your computer -- try
node -vto check - Make sure you have npm installed on your computer -- try
npm -vto check - Clone this repo to your computer
- Go to into the repo directory e.g.
cd intro-to-typescript - Run
npm install
Then each time you use it:
- You need two terminal windows open -- in both make sure you're in the repo directory e.g.
cd intro-to-typescript - In your first window, run
npm run build -- --watchThis starts a compiler that will generate Javascript files from the Typescript files, automatically updating them as soon as you save any changes to the Typescript.
If there are any errors in your Typescript code you will see them in this window. These must be fixed before you can run the compiled Javascript code in the step below (the Typescript code will only be compiled into Javascript if it's completely free of errors).
- In your second window, if you want to you can run the code in individual compiled Javascript files -- bear in mind you'll only see anything appear here if you've added console logging. To run a file compiled into Javascript from Typescript (most of the examples in this project), run the command
node lib/filename.jsand replacefilenamewith the particular file name you want to use e.g.node lib/1-typescript-strict.jsA couple of files in thesrcdirectory are just written in Javascript already, you can run those by doing e.g.node src/1-javascript-freedom.js
You don't need to run the code / it isn't useful for all of the files. A lot of this repo is just about looking at the code in your code editor. When you do run a file, you will only see any output if there are
console.logstatements. Make sure to use a.jsending as here we're running the compiled Javascript files, not the Typescript source (which is.ts) How we're doing this here isn't generally how you'd run your code in practice, it's just to give us a simple way to try out different things as part of this demo.