DEV Community

loading...

How programming is better with TypeScript ?

Kat Holder
“ In order to be irreplaceable, one must always be different”
・2 min read

TypeScript is a typed superset of JavaScript by definition. As a result, all current JavaScript is TypeScript compliant. Also, you may transform an existing JavaScript project to include type definitions and other language qualities that make long-term project upkeep and scaling much effective and simpler.

TypeScript includes a compiler that converts a .ts file (which cannot be viewed in a browser) to a .js file (which is vanilla JavaScript), as if we had written the entire project in JavaScript. It is quite useful to include this layer of 'convenience' above JavaScript code.

There has been a lot of comparison between TypeScript & JavaScript & also a lot of confusion as to which one to learn. However, in this article we will talk about TypeSCript & why it is loved by the programmers.

Key Concepts in TypeScript

There are three things that make Typescript so useful:

  1. Types
  2. Interfaces
  3. Classes and OOP

As a JavaScript developer, TypeScript will help you to:

  • boosting your confidence
  • catching errors before they reach production
  • making it possible to refactor code
  • saving you time from writing tests
  • giving you a good coding experience

When working with new codebases and in larger teams, TypeScript will give you more confidence. You will be less concerned if you are familiar with TypeScript and join a new team or project that uses it. You can count on TypeScript to help you out. Because you can look at something and quickly infer how it works, the language improves code readability and predictability. The type system is to blame for this.

Because TypeScript will detect errors for you, refactoring code becomes easy. When you rename a function, it will warn you if you fail to use the new name someplace else. TypeScript will correct you if you change the form of an interface or type and eliminate a property that you thought wasn't being utilised.

In essence, TypeScript is a JS linter. Alternatively, JS with compiler-friendly documentation.

As a result, unlike other languages like CoffeeScript (which adds syntactic sugar) or PureScript (which looks nothing like JavaScript), you don't need to know much to get started creating TypeScript code.

Types are unnecessary in TypeScript, and any JS file is a valid TypeScript file. While the compiler will warn if your starting files contain type problems, it will return a JavaScript file that works as it did before. TypeScript will meet you wherever you are, and it's simple to improve your skills over time.

Nevertheless, even if you don't use TypeScript to its full potential, it's a useful tool to have in your toolbox. It's simple to start small and gradually expand your capabilities, learning and adding new features as you go. There's no need to be terrified of TypeScript because it's practical and accommodating to beginners.

Discussion (12)

Collapse
spock123 profile image
Lars Rye Jeppesen

Once you go Typescript you never go back.

Collapse
maciekgrzybek profile image
Maciek Grzybek

💯 agree with that, at least with relatively bigger projects

Collapse
spock123 profile image
Lars Rye Jeppesen • Edited

I just converted a large backend API from JS to TS.
I cannot count how many bugs I could fix out of the box, just by all the places where non-existing object properties were accessed, function parameters were omitted etc.

Thread Thread
maciekgrzybek profile image
Maciek Grzybek

Exactly that :)

Collapse
adparris profile image
Andrew Parris

Agreed!

Collapse
krd8ssb profile image
Steven Brown

Once you really start using TypeScript, you'll never look at JavaScript the same. However, it's important to note that while it will help you identify bugs during development, it does not do runtime validations. When your application is being used after it has been compiled to JavaScript, TypeScript is no longer helping you so you still need to sanitize and validate your data.

Another thing to keep in mind is that TypeScript is not a replacement for tests. Unit tests, integration tests, contract testing, e2e testing, etc... are all still incredibly important and should not be skipped.

Collapse
itays123 profile image
Itay Schechner

100% truth.

Collapse
jfbrennan profile image
Jordan Brennan • Edited

50% truth.

all current JavaScript is TypeScript compliant

Not true. I’ve turned TS “on” in more than one project and it has refused to compile without code changes.

  1. Types
  2. Interfaces
  3. Classes and OOP

JavaScript has types, and a dozen tools to validate them at runtime, as well as classes and OOP.

saving you time from writing tests

That’s not true with TypeScript or any other strictly-typed languages, even Rust.

you can look at something and quickly infer how it works

Many many types end up being weak little abstractions of otherwise very simple objects. The excessive abstraction makes the codebase harder to infer how things work.

And about the multiple points about refactoring being easier. That's truthy, but this is only a win in the sense that poorly designed software is a little easier to maintain. If there’s any doubts after a refactor, it is a sign of poor design regardless of language.

Collapse
itays123 profile image
Itay Schechner

Well, I'm afraid I disagree. By "TypeScript compliant", they mean you can import javascript files to typescript and the other way around, but not nessecairly each piece of Javascript code will compile.

Regarding the Javascript classes - coming from a Java background, the value of Javascript classes is worth less compared to Java. Typescript offered the OOP features of Java with the flexibility of Javascript.

About your other points - direct them to the author:)

Collapse
necross220 profile image
Obed David Pérez Uri

Agree many dev blame Javascript specially devs coming for c# and asp. Net the reality is that you don't know how to program anything that doesn't take your hand all the way long... Same history is true for php and python many devs hate them but it is because it will not hold your hand "you are a developer make and store you code for future use and solutions" it has been said

Collapse
sturzaam profile image
Sturzaam

Hopefully this is just bad English.

"saving you time from writing tests"

Nothing, except ignorance, should save you from writing tests! If you're not writing tests, then don't push.

Collapse
adparris profile image
Andrew Parris

As most likely do, I struggled with TypeScript at first. But now, several months in, I kind of feel naked and unprotected without it. It's my default now.