Hacker News new | ask | show | jobs
by sonaltr 2710 days ago
I like TS. I really do. It makes life nice and easy to catch bugs quite quickly and also makes you think about your code quite a bit more (which I guess makes you a bit slower but it's important enough so it's ok).

What is annoying is when you are stuck on something and can't continue because you can't figure out the type of a specific object (and you've disabled any / or you are on the strictest settings).

I was recently stuck on ReactJS + Redux + Redux Saga and it took me a while (~ 1 day) to figure it all out (and I'm still not 100% sure if I did it right). It was fine when I disabled the strictest settings for a bit but it's definitely annoying (asking for help in Typescript, React, Redux, Saga and elsewhere didn't really help at all).

3 comments

I basically do two things to make sure type checking doesn't slow me down:

1) Use emitOnly: true. This means that if your code has type errors, it still compiles. And you can fix the type error later.

2) Never use any directly. Not all anys are equal. Some are there because you don't have the time to figure out a proper type annotation. Others are there because you can express a proper annotation, but think that it's just not worth the effort. And some anys are there because the type system is not capable of expressing the type you have in mind.

What you want to do is to clearly annotate your intention when you're typing something as any. So what I do is to simply disallow directly using any, and instead, use a few global type aliases that better communicate my intention:

  type $FixMe = any // Fix this type, preferably before accepting the PR
  type $IntentionalAny = any // This `any` is intentional => never has to be fixed
  type $Unexpressable = any // TS cannot express the proper type atm
I often put these aliases in a defs.d.ts file and use them instead of any.
> What is annoying is when you are stuck on something and can't continue because you can't figure out the type of a specific object (and you've disabled any / or you are on the strictest settings).

In VSCocde I can hover over variable names to display the type of an object in a tooltip. I assume this information is available for other editors as well via the typescript language server.

Also in VS Code you can click F12 to go to the definition. When importing something new, I will often do this & then spend time reading through all of their type information. This often makes amazing documentation!

When TypeScript first came out, this really helped me learn JavaScript & DOM types that browsers treated differently. It also helped me learn to write cross browser compatible JavaScript without depending on jQuery.

What is annoying is when you are stuck on something and can't continue because you can't figure out the type of a specific object

Funny, but that was the biggest impediment to big refactorings when I've worked in dynamically typed environments without type annotations. (A decade in Smalltalk.)