Learning about TypeScript interfaces

We did our first major work project using TypeScript and it's been pretty great. So great, I've decided to use it on my latest side-project as an additional learning opportunity.

One thing that confused me a bit at first was the difference between Interfaces and simple types.

For example, let's say I have a simple type like this

type Record = {
    name: string;
    value: string;
    num: number;
};

That's pretty straightfoward. I can declare a variable like this

const record: Record = {
    name: 'foo',
    value: 'bar',
    num: 100
};

and I'll get helpful errors if I set an invalid property or value.

The problem now is that I can't reference any properties of record by index!

for (const k in record) {
    if (record.hasOwnProperty(k)) {
        console.log(record[k]);
    }
}

The above will give me a TS error like No index signature with a parameter of type 'string' was found on type Record.

This is where Interfaces come in handy. With an Interface, I can explicitly type how an object can be indexed.

interface IRecord {
    [index: string]: number | string;
}

This simple interface says that I can index my object using a string (and nothing else! indexing by numeric index will throw an error). It also says possible return values can be numbers or strings (since those are the possible types of my current properties).

It's a good start, but I still don't have anything preventing me from assigning random properties or properties of invalid types on my object.

Luckily, Interfaces let you specify properties just like the simple type I defined in the beginning.

interface IRecord {
    [index: string]: number | string;

    name: string;
    value: string;
    num: number;
}

Now I can define my record object as const record: IRecord, my object properties are typed, and I can index the object by string.

How to get a driving road test during a pandemic

My middle son is currently eligible to take the road test1 for his driver's license!

The problem is that we're in the middle of a global pandemic so Goverment services are spotty at-best. First the DMV was closed for months, now there's a huge backlog of people trying to get road tests.

The timeline of our attempts went roughly like this:

  • Jul 2020 - Check the scheduling website and see they're booked through the end of Aug 2020
  • Aug 2020 - Find out that Sep 2020 tests become available on Aug 14
  • Aug 13, midnight - Go to scheduling website to find it's crashed.
  • Aug 14 - 16 - Periodically check scheduling website to find it's either down or still only showing Aug
  • Aug 17 - Site is up and showing schedule for Sep 1 - 18 and they're all booked 😡

At this point, I do what any self-respecting programmer would do. I write a script to automate this whole process.

The idea is something like this:

  1. Grab the contents of the scheduling webpage
  2. Check to see if there are any open days
  3. If 2, send me a text message
  4. Repeat every few minutes

There are obviously a million ways to go about this so I decided to go with my old standby, PHP2.

Here are 3 libraries that made this little script a breeze:

And here's the code which took less than 20m to cobble together.

The fun part is, I never even had to run it as a cronjob.

On the third manual test, I got a hit for availability on Sep 15, 2020!


  1. A little background for anyone outside of the US (or Kentucky).

    Here in Jefferson County Kentucky, you take a written test to get your driver's permit at age 16. With the permit, you can drive as long as there's a licensed adult over 21 in the car with you.

    After 6 months of driving with your permit and logging 60h of driving time, you're eligible to take the road test where you have to drive a car with a DMV (Department of Motor Vehicles) employee with you and complete certain tasks (parallel parking, etc). Once you've passed the road test, you get your provisional driver's license which lets you drive without an adult (with some restrictions) 

  2. Yeah, I know PHP isn't cool. Most of the stuff you hate about it hasn't been an issue since PHP 5 and PHP 7.4 (especially using something like Laravel) is downright tasty. Whatever. You do you. 

All Posts