A few months back I started using TypeScript on the Silicon Valley Code Camp New Web App (It’s written using React now and is mobile responsive based of bootstrap). For everyone, it’s different but here are my 5 steps of coming to terms with TypeScript (so far).
I have a type interface that I’ve created that basically looks like this:
I’ve recently added the optional null to all the non key attributes using the
?. The reason I did this is because I’m creating a new object of type
Session and I want to mention some but not all of the attributes. That is, something like this:
Sadly, that broke a lot of my code that now I have to fix. For example, I have this sort method I call with
title and now it’s causing a compile error because it could get a null value and throw an error. Here is the original sort method:
Obviously, the reason is that now that the passed in
My first inclination (which makes my app keep working) is just to change the type of the incoming parameters to
any. That would look like this:
The problem now is that though this compiles, it will crash at runtime if
room is null. We all know what this means.
Debt goes up, happiness goes down.
The right thing to do is this (and then right a blog post about it so it doesn’t feel like a total waste of time).
Thoughts? Am I crazy?
What will be my steps 6, 7 and 8 in my TypeScript Journey?
Hope this helps.