DEV Community

Discussion on: Is 2019 the year of TypeScript?

Collapse
 
seangwright profile image
Sean G. Wright

I get the feeling Martin meant 3rd party libraries that don't have typings defined (if written in JavaScript) or are written in Typescript but don't provide their own robust types for APIs.

This can definitely happen, though it's a lot less common (and painful) than it was 3-4 years ago.

I recently used an Angular-specific 3rd party library for authentication. They didn't include types for some of their underlying JavaScript code.

I had the option of either using :any as the type (or leaving the type off, as it will default to :any) or writing my own types to describe their JavaScript APIs.

I wrote out some type definitions that covered the part of their APIs that I was using and the definitions took about 15 minutes after looking at the JavaScript code in the package under /node_modules.

If you decide to use :any (or, again, leave off any type annotations, which works fine) you are where you would be with plain JavaScript - so nothing lost.

If you add type annotations, you gain all the benefits of types - my favorite being discoverability of APIs.

Thread Thread
 
totiiimon profile image
Totiimon

Right, It made more sense after reading more about it. I think the discoverability in general is such a nice thing you get by going with static types that is almost worth it by that alone haha

Thread Thread
 
seangwright profile image
Sean G. Wright

Another nice thing about Typescript is how the types "flow" through the application.

Often I only need to annotate a couple of parameters, variables, methods.

Then when using those things the rest of my code is able to infer the results of operations.

For example:

function getName() {
  return 'lalo';
}

const name = getName();

Notice, there are 0 type annotations here...

But, Typescript knows that name is a string and anywhere I use name, either as a parameter or in an assignment or expression, Typescript will flow that string type to the next place I'm working with name.

A lot of developers when first using Typescript will add a ton of annotations to their code, only to realize later that Typescript is great at inferring type information and doesn't need us to be so explicit.