DEV Community

Cover image for Pitch me on TypeScript
Ben Halpern
Ben Halpern Subscriber

Posted on

Pitch me on TypeScript

Part of a new series! Feel welcome to dip in and weigh in on a past question.

Let's say I've never used TypeScript before. Can anyone give the run down of what the language does and why you prefer it? Feel free to touch on drawbacks as well.

Latest comments (48)

Collapse
 
netanelhaber profile image
Netanel Haber

I wrote about this actually! (Tl;dr: Writing in TypeScript is like moving your js code from notepad to an IDE: You get refactoring, documentation, code completion and more): netanel.dev/blog/TypeScript,%20Typ...

Collapse
 
deltd3v profile image
deltd3v

How many of you have ever built very VERY large Web dev projects? 🤣

It's incredibly frustrating if you're only dealing with JavaScript.

Collapse
 
drsensor profile image
૮༼⚆︿⚆༽つ • Edited

Only one word.
"King of Web"

Although there is many alternative™, you can't call yourself web developer without knowing Typescript 😂

™: Hagel, Flow, dart2js, ReScript, Elm, PureScript, anything else?

Collapse
 
ikembakwem profile image
Ikechukwu Mbakwem

Typescript union types is a blessing in disguise. Very useful when you are expecting an input which might be of different types. Say for instance, you are expecting a number as input. You don't have control over the input type, it might be a string or an int. You can declare a union type as follows:
type stringOrNumber = string | number ;

Then you can use the variable in whichever way you want, knowing fully well that what you have is a string or an integer. Say an input of array was passed, you'd get a typed error and the code won't run. Saves you a lot of time ;)

Collapse
 
latobibor profile image
András Tóth • Edited

Imagine you are a new recruit on a big project written in Javascript. The task is simple:

  • Can you please change address of users so it is no longer just a string but it is an object containing zipCode, street, etc.?

You then start a Find in files search for the word: address.

Oh no. 😐 More than 200 results have come up. Some are comments, some are related to users, most are not. Some did not even come up as some folks made a typo: adress. You don't know until you have read the code for every occurrence of every address string. Good luck!

What if someone could have given you a crutch during development time: some extra annotation to know which address-es are related to users and which are not? To encapsulate in some meaningful type. To know the interface for every function in the codebase... Something that you can erase so you can stay compatible with the demilitarized zone of the frozen fronts of Browser wars: the one allowed language: javascript.

This is TypeScript. A tool, just like your IDE that can save a tremendous time if you learn how to handle it. If you write JS... Hey you already use it! Remember that nice library which your IDE immediately suggested the right object, function, parameter? Yes, the library had provided its typings to you. That's why it's so nice.

Don't be just some any when you can be somebody. Use TypeScript today!

Collapse
 
codingjlu profile image
codingjlu

TypeScript is fun to read. TypeScript is not fun to write.

Collapse
 
elsyng profile image
Ellis • Edited

I see TS merely as a tool for JS, and not a superset or flavour of JS. Use it sparingly, and it is useful. Don't take it as a religion. TS has plenty of very obscure features, they sound like voodoo, it's not good.

TS: don't use everything it offers just because it is there. Same goes for JS.

Collapse
 
jzombie profile image
jzombie

Makes refactoring a hell of a lot easier.

Collapse
 
charlesfries profile image
Charles Fries

Pitch me on JavaScript

Some comments may only be visible to logged-in visitors. Sign in to view all comments.