Advanced Typescript

Typescript is a very nice and pragmatic addition to Javascript, and enables a bunch of nice programming safety features that would be more difficult to implement without it. I’ve been reading the book Effective Typescript recently, and I thought I’ll start a short series on the tips and tricks I’ve picked up from it. So here’s part 1 of a 5 part series on some advanced things you can do with Typescript.

To Follow Along

Please install ts-node and typescript globally:

npm install ts-node typescript -g

& then just ts-node should give you a terminal:

❯ ts-node
> 1 + 1
2
> const a = "hello"
undefined
> a
'hello'

You can use this to get a Typescript REPL & then follow-along with the rest of the examples.

typeof operator

typeof is a Javascript construct that gives you a type of an object. If you use this in Javascript, then you get simple Javascript types:

> const a = {hello: "world"}
undefined
> typeof a
'object'

But in a type context, the same operator can be used for much richer type information:

> let b: typeof a;
undefined
> b = {hello: 1}
[eval].ts:4:6 - error TS2322: Type 'number' is not assignable to type 'string'.
4 b = {hello: 1}
       ~~~~~
  [eval].ts:1:12
    1 const a = {hello: "world"}
                 ~~~~~~~~~~~~~~
    The expected type comes from property 'hello' which is declared here on type '{ hello: string; }'

As you can see, using typeof as a type annotation on the left side of the new variable b produced the inferred type { hello: string }, a much more complex type than just object and Typescript good at this kind of inference. It works for more than simple variables too:

> let isNegative = (b: number) => b < 0
undefined
> let isPositive: typeof isNegative = "hello"
[eval].ts:7:5 - error TS2322: Type '"hello"' is not assignable to type '(b: number) => boolean'.
7 let isPositive: typeof isNegative = "hello"
      ~~~~~~~~~~

Here we see that we can use typeof to infer the type of a function. This can be used in several different places, and even when you import values or functions from an external library that has types.

Constructing Partial Types with Pick and Omit

You can use Pick and Omit to construct subset of types pretty easily:

> let metadata = {
... isOpen: false,
... isDraft: false,
... isComplete: true
... }
undefined
> type Metadata = typeof metadata;
> type SaveMetadata = Pick<Metadata, "isOpen" | "isComplete">

Here we’re defining a new type SaveMetadata as being derived from Metadata but having picked up only the isOpen and isComplete keys. This saves duplication & makes the code a lot more DRY. Omit is the reverse:

> type SaveMetadata = Omit<Metadata, "isDraft">

These are the same types written in two different ways.

That’s it for today! In the next part, we’ll cover keyof and indexed keys for deriving types based on object keys in Typescript.

Remote Music Production

There have been many examples of music being produced by people working together from different parts of the world. It’s fascinating isn’t it that what Corona has driven us to do. Here’s a few examples:

A beautiful rendering of Pagannini
Here’s Ode to Joy, like you’ve never heard it before 🙂

And now this, the new Star Trek Discovery score will be produced remotely.

What we are doing is recording individual session players from here in Los Angeles. They are fantastic and most of the people I have used on the Star Trek scores and Picard scores and Umbrella Academy scores. So we will record each individual in their home and then combine all of it to make the orchestra, so we don’t have to put everybody in the same room.

It is not an ideal situation, but it is working. We were very hopeful at the beginning that it is going to work and started small. We started with 4 and then went got to 8 people and went to 10. And now we are about to go up to 26 people and then hopefully we will get to 35 or 40.

Of course, when I think of remote music, I should mention Smule:

A chorus with Elsa

Will this ever become the norm? Probably not. But very interesting nevertheless. What would be a good listening experience for live music look like?

Ways of Seeing

I came across this wonderful BBC documentary from 1972(!) that talks about what art is, and how to look at art in a pretty interesting way. It’s a good watch, unfortunately there are no legal ways to watch this if you’re not in the UK (as far as I know, please correct me if I’m wrong). There are some snippets available here that do give you a taste of the program, but it’s worth watching in full[1].

A still from Episode 1: Psychological Aspects

Some takeaways:

  • There is a difference between being nude, and being naked. Being nude is for somebody else to take a look at. Naked is just you being yourself. There is a wonderful segment in Episode 2 where panel of women talk about women in art.
  • There’s a bunch of really good observations in Episode 1, but what really stood out is how children could discern something from a painting that adults, busy analysing its deeper meanings, couldn’t.

[1] Cough. One site that may help.