DEV Community

decker
decker

Posted on • Edited on

Is programming in TypeScript simply another excuse not to write test in JavaScript?

I know TS and i know JS and I prefer JS over TS because of multiple reasons.

Reading posts where people tells us why it is a good idea to use TS ends with types makes programms more stable because the TS compiler helps to avoid errors at build time.

But this can even better be done with tests, I think.
Tests can describe behaviour and the structure of your JS and that includes the types.

If you add an attribute to an object, your tests should show the problem and so on.

Top comments (5)

Collapse
 
ianito profile image
Iaan Mesquita

I believe that using Typescript goes beyond avoiding mistakes in type checking.
Tests make sure that your logic doesn't have unexpected behaviors (for example, when you are making refactoring) and we have a cost to doing this, especially in big projects.
But, using Typescript guarantees that you can see the errors before running the tests, better writability (ok, sometimes can punish readability), easily use new features, and get more productive for you and your team. (Generics, Unions, satisfies operator, etc)

Furthermore, javascript still is a valid typescript code, so...

However, if your team is more comfortable using Javascript that's what matters

Collapse
 
brense profile image
Rense Bakker

No. You should still write tests in typescript to test the relevant functionality of your app. What you don't have to do is add a bunch of conditions in your code, because you can't rely on a variable being what you think it's going to be. As a result you also have to write tests for those conditions, because you might make a mistake if you have thousands of such conditions in your code. I will also guarantee you that you haven't thought of a scenario that you should have to avoid runtime errors and as such its only a matter of time until your users do something that you didn't expect and your app crashes. You can avoid all that, by simply having a typed variable, but its up to you. For me personally its pretty obvious what is the better solution, but then again some people like to torture themselves 🤷

Collapse
 
romeerez profile image
Roman K • Edited

No one says TS eliminates the need of tests.

TS helps with tests, so when I change some type, the editor highlights all places in tests to be fixed, and when there are hundreds of tests it saves a lot of time. And sometimes, I don't even run the tests to start fixing them, but quickly updating the code guided by the editor and run them afterwards to see all green.

So TS doesn't guarantee you won't have bugs, but it helps to make updates faster.

Collapse
 
nasheomirro profile image
Nashe Omirro • Edited

I think they solve different problems, though typescript does catch errors like tests do, it provides other features. In the scenario you gave, I think typescript is better because it tells you exactly where the error occurs by highlighting. Whilst tests does tell you where the errors are too, you have to first run them and that could take a few seconds each time you do, And also that you need to write those tests.. wait-

Collapse
 
tombohub profile image
tombohub

Not for me, I don't write tests either way. With Typescript computer works for me, not me for computer.