Fork me on GitHub

Project Notes

#294 TypeScript

TypeScript - my tl;dr summary, plus notes on getting started on macOS (Apple Silicon) and Ubuntu (x86).

Notes

TypeScript is a strongly typed programming language that builds on JavaScript. The main benefits it claims are:

  • strongly typed programming language
  • compiles down to standard Javascript that can runs anywhere JavaScript runs
  • next-gen Javascript features compiled down for old browsers
  • non-Javascript features like interfaces
  • meta-programming features like decorators
  • rich configuration options
  • modern tooling the helps even in non-TypeScript projects

Installation - macOS (Apple Silicon)

I use Homebrew to manage software installations on my Mac. I’ll use it here to install the node pre-requisites for typescript.

$ arch
arm64
$ brew upgrade node
...
$ node -v
v22.5.1

Installing TypeScript, local to this folder

$ npm install typescript --save-dev
$ npx tsc --version
Version 5.5.4

Installation - Ubuntu (x86)

I’m currently running Ubuntu 22.04:

$ uname -v
#44~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Jun 18 14:36:16 UTC 2

The version of Node.js included with Ubuntu 22.04, version 12.22.9, is an LTS release. It is technically outdated, but should be supported until the release of Ubuntu 24.04.

To install node v22, I’m using nvm:

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
..
$ nvm install 22
..
$ node -v
v22.6.0

Then installing TypeScript:

$ npm install typescript --save-dev
$ npx tsc --version
Version 5.5.4

A Simple Example

A simple TypeScript example is in hello.ts demonstrates custom types and typed parameters:

type User = {name: string; age: number};

function greet(user: User) {
  let message: string = 'Hello ' + user.name + ', you are ' + user.age + ' years old';
  const output_element = document.getElementById('output');
  output_element.innerText = message;
}

const user1: User = { name: 'Paul', age: 29 };
greet(user1)

Compiled with tsc to produced the generated Javascript file hello.js:

npx tsc hello.ts

And then executed in a browser with a simple wrapper HTML file hello.html:

hello

Credits and References

About LCK#294 typescript

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

Project Source on GitHub Return to the LittleCodingKata Catalog
About LittleCodingKata

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.

Follow the Blog follow projects and notes as they are published in your favourite feed reader