DEV Community

Discussion on: Is 2019 the year of TypeScript?

Collapse
 
mrtnrst profile image
Martin Arista

I do believe it's all the rage right now but there are times when I see the value in a JS implementation. I still think there is not enough support for certain types and you are left either writing your own adding any to describe something. And in converse, when working on a large codebase or teams the comfort of types is very helpful.

And naturally a shameless plug for a react-native expo starter I made to solve that problem for RN.
github.com/mrtnrst/expo-typescript...

Collapse
 
chiangs profile image
Stephen Chiang

I'm curious as to what types you're missing as well.

I have not encountered that yet. An interesting quirk is that if you only need Type checking then you should make an interface. Interfaces are thrown out at compile time which leaves your code lighter. Classes are kept and should only be used if you actually need to instantiate an instance of that type during runtime.

Collapse
 
totiiimon profile image
Totiimon

Which are some types you think are not supported? I was interested in jumping in this TS-hype-train and things like this are good to know before :)

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.