DEV Community

loading...
Cover image for TypeScript and why you should type your JS project

TypeScript and why you should type your JS project

migueldevelopez profile image MiguelDevelopez Updated on ・5 min read

TypeScript is like JavaScript but with no surprises.

I heard a long time ago about TS, great stories about how useful it is, allowing js to have Javascript types. At this moment I wasn’t so 100% aware of the sense of type everything, now that I have more knowledge about clean code, good practices and start to develop some little applications in React I think that the code could be better with types and I decided to investigate about and… this is what I found:

🥰DEVELOPERS LOVE IT

Here are some charts about how people start to use more and more…
image
More interesting charts about JS here: https://2020.stateofjs.com/en-US/technologies/javascript-flavors/
Increasing the usage in GitHub projects.
image
https://octoverse.github.com/

And high place in the 2020 developer survey of StackOverflow:
image
https://insights.stackoverflow.com/survey/2020#technology-most-loved-dreaded-and-wanted-languages-loved
Big companies like Airbnb support its use and claim that using it would significantly reduce potential errors.
image
Source: https://twitter.com/swyx/status/1093670844495089664/photo/2

I went deep into Reddit to find some real words about it and the same: “It's very hard to go back to Javascript once you start writing Typescript”.

🚄But… WHY ALL THIS HYPE?

With so many people loving it I decided to learn the basics and this is what I have learned that it can offer.

Advantages:

  • SUPERSET of JS, almost the same language but with consistency in types.
  • The main advantage is strict typing, you can type everything, from variables to the parameters of a function, and even the structure of an object, that's can prevent about 15% of the bugs that end up in committed code.
  • The ability to find these obvious yet frequently occurring errors this early makes it a lot easier to manage your code with types.
  • Types make code management easier and more predictable.
  • In TypeScript we can define interfaces for parts of our program, so we can be sure that they interact correctly. It means they will have clear contracts of communication with each other which will significantly reduce the number of bugs. TS + unit tests will do the code more stable, predictable and it will reduce the amount of pre-release bug density.

But… Not all that glitters is gold...

Some little disadvantages:

  • Apply type may make you slower in the first instance, in the long term it is better but you have to get used to it.
  • Required compilation.
  • And of course, TypeScript does not catch run-time type errors. It means that you can write the code that will pass the type check, but you will get an error upon execution.

🤖 Examples of SYNTAX

Basic typing when you declare variables, you won't be able to change the type later (even if you don't declare strictly the type it will be typed), making your code more reliable:
image
image
image
image
If for any reason, you need a variable without specifically type you can use any:
image
But... in the documentation, they recommend not to use it unless you are in the process of migration from JS to TS.
image
Add enum to JS, a way of giving more friendly names to sets of numeric values:
image
Typing parameters of a function will allow you to detect quickly if you insert something wrong
image
image
You can add what is the type of function that will return
image
image
but if you don’t do it typescript will do it for you
image
You can create your own types that allow you to don't repeat the code.
image
You can type also what you select from the DOM, allowing you access to all the methods of one kind of input.
image
There are also interfaces, a contract that should be matched with the object if you don’t want errors, and other
image
And of course, you can implement TypeScript into your favorite JS framework, I'll show you an example in a React project, providing the same advantages mentioned above:
image
Similar to the PropTypes and now you will have to add the props as mandatory when you use them or the IDE will warn you:
image
And when you introduce the mandatory prop person it will be okay:
image
You will be able to type other things (practically everything) like, for example, the hook useState:
image

And that’s it, folks, I hope you enjoy this little piece of info about TypeScript and maybe consider applying it in some project.

Here are some sources and interesting videos to check and go deeper if you want:
JSConf - Airbnb tactics and strategy to migrate to TS (very interesting)
https://youtu.be/P-J9Eg7hJwE

To type or not to type: quantifying detectable bugs in JavaScript
https://blog.acolyer.org/2017/09/19/to-type-or-not-to-type-quantifying-detectable-bugs-in-javascript/

Official documentation - TS in 5min to JS developer:
https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html

TypeScript tutorial series:
https://www.youtube.com/playlist?list=PL4cUxeGkcC9gUgr39Q_yD6v-bSyMwKPUI
In this video, you can see how to apply TypeScript in React:
https://www.youtube.com/watch?v=Z5iWr6Srsj8

Discussion (101)

pic
Editor guide
Collapse
michaelgrigoryan25 profile image
Michael Grigoryan • Edited

To be honest, as a person who knows Kotlin, I wouldn't use or even try to pick up TypeScript(the syntax is mostly the same(yet some things are different(for example: in TS I don't have to type the returned value by a function whereas in Kotlin I do))). Yes, it's a cool thing nowadays, still I don't want to overcomplicate/overconfigure my projects too much, yes it does offer strong typing, yes it does transpile to JS, yes it does many other things...
But still I wouldn't use it in any of my projects (at least I hope so 😅)

Collapse
ssijak profile image
Saša Šijak

Then you just don't have experience of working on large enough plain javascript object. Jumping into such project and changing anything is a scary proposition.

Collapse
peppesilletti_4 profile image
Giuseppe Silletti

The only thing that will back you up when making changes is having in place some nice, solid automated tests.

Thread Thread
michaelgrigoryan25 profile image
Michael Grigoryan

Strongly agree on this one

Thread Thread
clay profile image
Clay Ferguson

Tests can never offer 100% coverage. Compiler checking CAN offer 100% coverage. You have two choices;

1) Catch typos at compile time.
2) Catch them at runtime.

Thread Thread
peppesilletti_4 profile image
Giuseppe Silletti • Edited

Type correctness does not guarantee program correctness.

You need tests anyways, types or not types. If there's a typo the tests will fail, so in this specific case I'd say that types may be redundant.

Thread Thread
clay profile image
Clay Ferguson • Edited

I'm not trying to be rude, but saying tests are a replacement for Type Safety in languages is not just wrong but I'd even say hilarious.

Thread Thread
peppesilletti_4 profile image
Giuseppe Silletti

Where did I write that?

Anyways, at least for TS, having the right static type doesn't mean having the right runtime type. So you need to write tests for that. And those tests will also catch the same bugs that the compiler could catch.

Thread Thread
clay profile image
Clay Ferguson

Hey man, if you think tests are the way you'd like to find your typos, more power to ya. lol.

Thread Thread
peppesilletti_4 profile image
Giuseppe Silletti

Hey man, if you need to introduce a whole new language only for catching typos, good for you! I'll keep using the eslinter

Thread Thread
clay profile image
Clay Ferguson

eslinter can't do 1% of what TypeScript does. Try again. I use TS and also eslinter. They're different tools for different purposes.

TS can prove the non-existence of typos. All tests can do is prove it didn't FIND them. lol

Collapse
clay profile image
Clay Ferguson

On small projects you can get by without type safety, but in very large projects, the fact that TypeScript can pick up 99% of the bugs at compile time (not to mention the refactoring support and other advantages), is indispensable, and working without it is like being in the stone ages.

Not being able to catch 99% of your bugs at compile time (because you just don't want to) is just shooting yourself in the foot for no reason. Also anyone claiming TS adds "bloat" just doesn't yet "get it". If anything TS will make you have smaller code not bigger code than pure JS.

People who are against TS in general came to that conclusion first and then looked for reasons to justify it second. I've never seen someone argue against TS who wasn't in some way confused, or had already made up their mind before even weighing pros and cons.

Collapse
matthewbdaly profile image
Matthew Daly • Edited

OK, I'll bite.

Flow offers many of the same advantages as Typescript. You can specify the shape of objects, define common interfaces, and so on, and Flow will pick up on type-related errors for you.

I'd also argue that using a language that extends Javascript is potentially risky. You can always bet on Javascript being available in the future, but languages that compile to Javascript have a rather shaky history (can't be too much support for Coffeescript left, for example). Plus, Microsoft's track record on embrace, extend and extinguish is something to be wary of. By contrast, Flow was always implemented as just a set of annotations on top of Javascript that can be stripped out by a simple Babel transform.

For those reasons I opted for Flow over Typescript, and I have no plans to change anytime soon. I'm of the opinion that it's a better solution to the same problem, and provides similar benefits.

I will say that I regard both Flow and Typescript as transitional solutions - adding equivalent functionality to the Javascript language would be a far, far better solution.

Thread Thread
clay profile image
Clay Ferguson

I never compared TypeScript to other type-safe languages, because I was only making the case against the argument people make saying they just don't even need type safety in languages, period.

Sounds like you're on the "Type Safety is good team". That's good.

However TypeScript has definitely "won", in it's arena. There are no risks involved in using TypeScript that would make it like Coffeescript example. AND even if TypeScript did disappear some how, it's the CLOSEST thing to JS so there's no other solution better even in that unlikely scenario.

As far as JS some day becomming "TypeSafe" language, I doubt that because everyone in the world is already using TypeScript for that. It's more likely that browsers will just start "accepting" TypeScript naively, but with Flow that's billions of times less likely.

Collapse
michaelgrigoryan25 profile image
Michael Grigoryan • Edited

Then I'm not in that group of people 🙂

Collapse
patarapolw profile image
Pacharapol Withayasakpunt • Edited

I use TypeScript (and Prettier) instead of ESLint and Babel, actually. And tsconfig is quite simple and intuitive.

Though, of course, you can use all of these together.

I wouldn't use uncompiled vanilla JS, as it is inconsistent between browser and node (as well as unminified and unfeatured).

So, if compiled anyway, why not TypeScript?

Collapse
michaelgrigoryan25 profile image
Michael Grigoryan
  • you have to type everything
  • you have to install types for a package and if a package doesn't have it you won't be able to fully benefit from it And more
Thread Thread
patarapolw profile image
Pacharapol Withayasakpunt • Edited

It doesn't matter if I don't fully benefit from it, does it?

Don't be too serious and try to fight JavaScript.

Furthermore, I don't have a problem creating my .d.ts.

Thread Thread
michaelgrigoryan25 profile image
Michael Grigoryan • Edited

That's the problem. You're unnecessarily overcomplicating stuff when using TS in development. In JS you don't have to do anything(mostly)

Thread Thread
migueldevelopez profile image
MiguelDevelopez Author

I don't get the negative point of type everything, it only costs you a few seconds

Thread Thread
michaelgrigoryan25 profile image
Michael Grigoryan

Yes Miguel, sometimes it is some seconds sometimes it can be 5 minutes and I don't want to waste time on typing things, that's just my opinion

Thread Thread
clay profile image
Clay Ferguson

It's a common misconception that in TypeScript everything is and MUST be typed. That's the opposite of the truth. TS lets you still write 100% JS and untype everything "if" you want. But once you add a type to an object or function, then it starts checking it for you wherever you use THAT thing. It's the perfect solution.

I've found one thing in common among everyone who doesn't like TS, which is: They generally don't even understand it yet.

Thread Thread
michaelgrigoryan25 profile image
Michael Grigoryan • Edited

At least I know that it's not true for me, because I was trying it for several months and it didn't make any impression on me.

Thread Thread
clay profile image
Clay Ferguson

That's why TS is called a "superset" of JS. If you just write all your code as pure JS the TS compiler handles it just fine, and then as you start adding more and more types, TS starts checking them. Once you add types to ALL your code, you're DONE with the transition and typos are a thin of the past.

Thread Thread
michaelgrigoryan25 profile image
Michael Grigoryan

Yes and that's overcomplicating. You add the types instead of doing the actual development. And as I see you're protective of TS in many posts/comments.

I don't want to continue this conversation any further.
My opinion stays the same.

Thread Thread
patarapolw profile image
Pacharapol Withayasakpunt • Edited

I like to add typings in JSDoc (for JavaScript) and typings in Python as well.

You can do it to that level in TypeScript by setting noImplicitAny: false in tsconfig.json; and then, you don't have to always add typings.

But no, it's not the default settings.

To stray you away from TypeScript further, I would say that, typing in any dynamic languages (maybe apart from Dart and Julia), might give you a false sense of security. You should still write tests after that.

Still, I prefer to work with JS users who at least write JSDoc.

Thread Thread
clay profile image
Clay Ferguson

Your opinion is clearly that having types in a language makes that language cumbersome because people spend too much time worrying about types.

You can take a poll of 1 million developers who have over 10yrs experience, and you'll find 99.99% of them are in favor of type safety. It's not a matter of opinion, quite so much as it is a measure of one's experience in the field.

Thread Thread
michaelgrigoryan25 profile image
Michael Grigoryan • Edited

Am I against type safety? No. Am I against typescript? Yes.
As a developer that also knows kotlin I think type safety is important but when it overcomplicates your project then it's a solid no from me. I have clearly stated in my initial comment that TS has many advantages but I won't be using it ever

Thread Thread
clay profile image
Clay Ferguson

Like I said in a different post if you're in high school now, just do what you love! Do JS if you like it! Don't worry about TS. None of my "opinions" apply to you, but only the professional audience.

Thread Thread
michaelgrigoryan25 profile image
Michael Grigoryan

That doesn't mean anything. I may be in school but I fully understand about the subject that I'm talking about. I have weighed up everything before making that comment.

I don't want to be impolite here but except the typos and typing there's no other reason to use TS.

For instance Giuseppe told everything that I wanted to, still you bring up the same reason to use TS which is typos.

Thread Thread
clay profile image
Clay Ferguson

Like I said, if you're in school still, JS is perfect for you. There's absolutely no reason you should worry about using TS. TS is only for professional developers where typos in code can literally cost $1000s in corporate revenue and company reputation.

Thread Thread
michaelgrigoryan25 profile image
Michael Grigoryan

There are many professional developers that use JS and have no problems with it

Thread Thread
clay profile image
Clay Ferguson

Good. Keep using JS.

Thread Thread
michaelgrigoryan25 profile image
Collapse
stojakovic99 profile image
Nikola Stojaković

You can set up ESLint to catch cases like this - then it will warn you to add return type to a function.

Collapse
michaelgrigoryan25 profile image
Michael Grigoryan

Thanks for the suggestion 🙂

Collapse
vbanditv profile image
Fima Taf

Wow, you just said everything that i think about typescript especially the comparison with kotlin.
I thought i was alone in this opinion but you prooved me the oposite, thank you!

Collapse
michaelgrigoryan25 profile image
Collapse
ssijak profile image
Saša Šijak

Then you just dont have experience of working on large enough plain javascript object. Jumping into such project and changing anything is a scary proposition.

Collapse
michaelgrigoryan25 profile image
Michael Grigoryan • Edited

I actually have worked with a fair amount of big objects(typically containing 50+ properties) and did not have any problems

Thread Thread
clay profile image
Clay Ferguson

In this context, "did not have any problems" is in every say synonymous with "never had any typos".

Even if someone can make the claim they never personally make any typos, they most definitely can not say "and neither does anyone else on my team." therefore...the "no problems" claim is a logical impossibility.

Thread Thread
michaelgrigoryan25 profile image
Michael Grigoryan

It doesn't have to. Yes I do sometimes have typos in my code which are easily debuggable with the built-in debugger and error logs. But I indeed didn't have any issues while deving with JS and if I do console is my best friend everywhere 🙂

Thread Thread
clay profile image
Clay Ferguson

I'm just saying that in a large project with many team members, denying that there's huge numbers of bugs that'll be caught right at compile time (rather than during testing by you or customers)...is just not a credible claim.

Thread Thread
michaelgrigoryan25 profile image
Michael Grigoryan

Well yes, but I can use VS Code extensions to do that for me instead of switching to a whole different language

Thread Thread
clay profile image
Clay Ferguson

I guarantee you VS Code can't find wrong types in code where no types are defined. :) That's like mining for gold in a bucket of water. Doesn't exist. Sure VS Code can do some things. But type-checking untyped code isn't one of them. lol.

Thread Thread
michaelgrigoryan25 profile image
Michael Grigoryan • Edited

Then I could use something like Flow or JSDOC which is a far better solution in my opinion. You just enter

/**
 * @type {Number}
 */
const s = 0;
Enter fullscreen mode Exit fullscreen mode

It does the same thing as TS does. Just without overcomplicating stuff.

Thread Thread
clay profile image
Clay Ferguson
const s: number = 0;
Enter fullscreen mode Exit fullscreen mode

is less complicated. :) I think you're trying to just avoid installing TS on your computer. Trust me it's not that painful, and does more than Flow, does it better than Flow, and does it using industry standards. TS is not just some "Third Party API" like you seem to thing. It's genuinely "The New Javascript".

Thread Thread
clay profile image
Clay Ferguson

The initial discussion wasn't Flow v.s. TypeScript. It was JS v.s. TS.

People brought up Flow because they realize the "we don't need type safety" argument didn't hold water. :)

Thread Thread
michaelgrigoryan25 profile image
Michael Grigoryan

I have tried TS in the past and I know how to use it, but IMO it's unnecessary. And yes I am avoiding installing TS on my computer because I don't think that TS makes my work any easier. Regarding to the JSDOC thing, it was an exmaple
and that doesn't mean that I use it every day.

Thread Thread
clay profile image
Clay Ferguson

I just now clicked your profile to see you're a high school student. Definitely for you, learn JS first. Don't worry about TS for yourself. TS is only necessary for professional teams... where typos in code simply should not be tolerated. Have fun codin'

Thread Thread
michaelgrigoryan25 profile image
Michael Grigoryan • Edited

I see that you did(splitbee analytics). And no my first programming language wasn't js it was python

Thread Thread
clay profile image
Clay Ferguson

Python is cool, for students to learn too! I started in Basic and Pascal in 1980!

Collapse
zhnedyalkow profile image
Zhitomir Oreshenski

For 'your' not for your client's projects, at least not for big projects :D other things are true

Collapse
Sloan, the sloth mascot
Comment deleted
zhnedyalkow profile image
Zhitomir Oreshenski

I can understand your point, the thing is if your client has already a project with installed ts there and with what type of client you are working on :)

Thread Thread
michaelgrigoryan25 profile image
Michael Grigoryan

I would stick to JavaScript, ESLint and Babel anyways.
My decision/opinion about TS stays the same. It's a unnecessary language (at least for me)
If that client wanted a TS project then he'll need seek for another person.

Thread Thread
zhnedyalkow profile image
Zhitomir Oreshenski

Well, most of projects are with ts. Almost each framework has ts integration as well.

Thread Thread
michaelgrigoryan25 profile image
Michael Grigoryan

Well it's the decision of that project I use what works best for me. I'm not going to use a whole another language just to avoid typos and bugs. It's unnecessary

Thread Thread
migueldevelopez profile image
MiguelDevelopez Author

No offense Michael, but you sounds so radicalized when you say:
If that client wanted a TS project then he'll need seek for another person.

What you said before is understandable but if your tech lead thinks TS is a good thing for the project you have to follow his lead if you know less than him, maybe open a debate but if the tech lead and the rest of the team agreed...

Collapse
matjones profile image
Mat Jones

TypeScript is like JavaScript but with no surprises.

Sorry but this is just utterly false for so many reasons.

Collapse
patarapolw profile image
Pacharapol Withayasakpunt

That's true. But then TypeScript is getting better.

The latest change is ability to make array and record indexing return undefined. (by settings in tsconfig.)

But yes, I will never trust TypeScript to be truly safe, nor truly strongly typed.

Collapse
matjones profile image
Mat Jones

Yeah, don’t get me wrong, TS is still a VAST improvement over JS.

Collapse
migueldevelopez profile image
MiguelDevelopez Author

and if you combine types and tests, will make the code reliable?

Thread Thread
matjones profile image
Mat Jones

If you have reliable enough tests, sure. And to be clear, I still think TS is a huge improvement over JS.

Thread Thread
johnkazer profile image
John Kazer

Having types of some sort helps me greatly in just writing functions, because it makes it much easier to remember just what data I'm messing around with. But I do tend to a functional/data-first style. So it reduces cognitive load way before you get to using tests/compiler to check things. And so, in fact, if your types are wrong your function/data manipulation will go wrong when you write it and a compiler/running tests are secondary things.

Collapse
clay profile image
Clay Ferguson

Uh, I've never seen TypeScript fail to find a missing parameter, misspelled variable, misspelled function name, wrong property on an object, etc. I think the fact that JS catches NONE of those things is what's meant by "surprises".

Of course anyone can come up with their own definition of "surprises" and then try to use that to claim someone else said something wrong. lol.

Collapse
johnkazer profile image
John Kazer

Could you list a few?

Collapse
matjones profile image
Mat Jones

Incorrect types for open source packages, happens quite often

If you’re targeting the browser... Internet Explorer

Generics not working the way you expect them to in certain scenarios

There’s lots. It’s also just a superset of JavaScript, so any surprise JavaScript can produce, typescript can also product.

Thread Thread
migueldevelopez profile image
MiguelDevelopez Author

Thanks for sharing these points, Mat!
I'll investigate the cons of using TS 🤔

Thread Thread
matjones profile image
Mat Jones

Don’t get me wrong, TS is a VAST improvement over JS, but it does have its own set of “gotchas”

Thread Thread
migueldevelopez profile image
MiguelDevelopez Author

To be honest, I don't use TypeScript yet, I just found it very interesting and loved by the community so... for that reasons I decided to write this, but I have in mind to start a little project to check his good points (and the bad ones too) 👏

Collapse
akashkava profile image
Akash Kava

ts-ignore is superior to as any

Collapse
patarapolw profile image
Pacharapol Withayasakpunt

You can ban both of these with latest ESLint.

Collapse
akashkava profile image
Akash Kava

eslint-disable is superior to ESLint, its really difficult to catch unless you setup automated git monitor to track commit and warn usage of such features.

Thread Thread
patarapolw profile image
Pacharapol Withayasakpunt

Nonetheless, the idea of you CANNOT disable Golint sucks.

Collapse
migueldevelopez profile image
MiguelDevelopez Author

You diabolical...

Collapse
lifelongthinker profile image
Sebastian

Nice one! I fully agree on all points. TS offers the best of both worlds: the dynamic features of JS and the static, strong typing of TS.

Bottom line: The compiler is our best friend, so let's not reject it 👍

Collapse
migueldevelopez profile image
MiguelDevelopez Author

Thanks, Sebastian!
I agree! I am looking forward to using it in some project

Collapse
andreimoment profile image
Andrei Andreev

I appreciate your taking the time to write this article - it is a brief and clear introduction to TS.

And, I find the fact that you call a function that adds two numbers "minus" confusing to the point of disturbing. Would you consider fixing the example?

Also, after reading the article, I still don't know why "I should type my next JS project".

What are some common patterns where using typed JS is a benefit?

I get that other devs like it, but why?

Thank you!

Collapse
migueldevelopez profile image
MiguelDevelopez Author

Thanks for the constructive critic, I will update the advantages of TS to make it more clear with some examples and statistics about bugs

I reply to another dev to a similar question so I will copy-paste the answer:
Yes, I guess you are right, I miss the point of the title during the process trying to explain how simple is to write in TS instead of JS.
As you can see with TypeScript we can define the interfaces for parts of our program, so we can be sure that they interact correctly. It means they will have clear contracts of communication with each other which will significantly reduce the number of bugs. TS + unit tests will do the code more stable, predictable and probably will reduce the amount of pre-release bug density.

Any suggestion to improve the example related to the function minus?

Collapse
andrei_says profile image
Andrei Andreev

Hi Miguel, thanks for the reply.

  1. Name the function “add” so that it does what it says.

  2. What are some common places in a web app which benefit from typing and make the time investment of using typescript worth it? Examples?

Most of us don’t get type errors in our code. TS is a significant overhead. Why the overhead if no benefit? Adding busy work does not make me happy.

Why are all these developers so happy? Code examples where the extra hours of using TS actually save time?

Collapse
sebbdk profile image
Sebastian Vargr

Having types means needing to write less unit tests.
And they serve as documentation.

There's really no reason to not use a typed language.
Typing is not what developers spend most of their time one anyways.

Collapse
sebbdk profile image
Sebastian Vargr

Actually, thinking about this again, there is one very good reason not to use types. :)

For people that are just learning to program, learning types is less important than learning the mindset & how to debug.

Gotta walk before we can run. :)

Collapse
darthwalsh profile image
Carl Walsh

I really like JSDoc! I've used TypeScript for a few libraries but for projects that don't need a build step, it's so simple to just edit your source from the browser devtools source panel.

You can still check your project "compiles" by running the typescript compiler over our JS source by adding tsc -p jsconfig.json to your package.json like this. Here's my jsconfig.json -- the commented out rules fail now but it's on my backlog to clean up the code.

Collapse
imsuvesh profile image
Suvesh K

After reading the whole thread my conclusion -
There are 2 types of people -
First - who likes TS / have used earlier and know how helpful TS is in Coding.
Second - Who don’t want to write extra pieces of code, So to justify their laziness they start giving excuses like Overcomplicated / Bloat / Better Alternatives / Example of closed projects.....

Nothing is perfect at the start, JavaScript also got multiple updates.
That's why we are developers, We evolve with time. If some new project comes that offers better than TS we will switch to it.
It's the same like - I will use Notepad because MS Word has spelling and grammar correction which is overcomplicated.

Collapse
abdurrkhalid333 profile image
Abdur Rehman Khalid

I started to use TypeScript when I learned Angular 4. At that time, I thought that I would be using TypeScript only for Angular Development, but as time progressed, the TypeScript found its way through the ReactJS and normal JavaScript Development as well.

I think the reason behind this is the strictness that do not allow the developer to get too flexible with JavaScript and allow the developer to write more readable and cleaner code.

Collapse
migueldevelopez profile image
MiguelDevelopez Author

Totally! That's one of the main advantages for me

Collapse
lesleyvdp profile image
Lesley van der Pol

Typescript is great in my opinion. I'm glad to be able to use it at my current client now. I do think the article focuses more on how you write typescript than explain why you should use it in your JS projects. Great article none the less! :)

Collapse
migueldevelopez profile image
MiguelDevelopez Author

Yes, I guess you are right, I miss the point of the title during the process trying to explain how simple is to write in ts instead of js.
As you can see with TypeScript we can define the interfaces for parts of our program, so we can be sure that they interact correctly. It means they will have clear contracts of communication with each other which will significantly reduce the number of bugs. TS + unit tests will do the code more stable, predictable and probably will reduce the amount of pre-release bug density.

Collapse
suneeleng profile image
Suneel Simhadri

But finally all .ts files will convert into .js files so they can work in browsers.
Then what's the exact advantage of Typescript.

I have coded something in typescript,
Let a:string = 'some string';

And I tried to change that value using input[type='number'] tag.
Then 'a' value is changing even the given value is 'number'.

Can you please give me more brief about typescript?

Thanks

Collapse
peerreynders profile image
peerreynders • Edited

TypeScript advocates always seem to be up in arms when somebody suggests that they don't need types.

I'd like to turn the tables. I'm flabbergasted (well, not really - more like 🤦‍♂️) how people claiming to value static typing are championing a tool that eschews soundness.

No Soundness, No Business

Now Java dates back to 1995 and C# to 2000 but TypeScript didn't appear until 2012 - so what is TypesScript's excuse? Gradual typing of JavaScript code. So that is worth a tradeoff that essentially undermines the perceived "safety" of static typing?

Bottom-line - anybody who is actually serious about static typing wouldn't be using TypeScript for this exact reason but something more like ReScript (and simply accept the additional pain of JavaScript interop).

TypeScript is simply a super linter that requires syntax extensions to normal JavaScript (making it not parsable as JavaScript unless you stick to JS Doc TS) to do its job. TypeScript is a Subset of JavaScript - it's only a superset in terms of syntax but not in terms of capability.

TypeScript is popular:

  • because of IDE (VS Code) integration, most notably Code Completion. It's humorous how for years Java users have been denigrated by their peers for their reliance on IDEs to work around the warts and boilerplate of Java - and TypeScript/VS Code are gaining popularity and praise for those same features - TypeScript could only succeed because VS Code (and the like) made the additional ceremony bearable.

  • because it gives developers coming from Java/C# a false sense of familiarity - which is actually dangerous because they keep operating on a mental model that is something other than JavaScript, setting themselves up for disappointment.

And finally the term "strong typing" is almost meaningless:

Erlang is also strongly typed. A weakly typed language would do implicit type conversions between terms. If Erlang were to be weakly typed we could possibly do the operation 6 = 5 + "1". while in practice, an exception for bad arguments will be thrown:

i.e. Erlang is dynamically typed but considers itself strongly typed because it does not coerce types.

When people use the term "strong typing" they usually mean "static typing"; when they use "static typing" they feel "safe" even though that safety actually requires "soundness".

Yes, types improve code.

It's usually helpful to write down the type of new functions first;

But the primary benefit comes from thinking and designing in types, i.e. type-driven development. That can be practiced in a dynamically typed language - simply using a type-aware tool doesn't automatically yield those benefits.


Life After Business Objects

Collapse
hardikchopra242 profile image
Hardik Chopra

Great Article ✨ , a must read!

Collapse
migueldevelopez profile image
Collapse
sempsei001 profile image
sempsei

verry good dude

Collapse
jwp profile image
John Peters

I love Typescript mostly for Typedefs which cover 95% of all JavaScript libraries. Immediate API discovery as I type.

Collapse
johnkazer profile image
John Kazer

I think I'm just gonna make the jump from using jsDocs for typing (no compilation so not as robust I guess but somehow nicer) to ClojureScript.

Collapse
patarapolw profile image
Pacharapol Withayasakpunt

It is also possible to use Babel, tscomb or Flow.

Collapse
lyrod profile image
Lyrod

Any one knows this font?

i.imgur.com/mUUZcnU.png

Collapse
andrewbaisden profile image
Andrew Baisden

TypeScript is going to gain more popularity this year.

Collapse
mzaini30 profile image
Zen

I think, I can't use TS in Svelte

Collapse
migueldevelopez profile image
MiguelDevelopez Author

It seems since July 2020 Svelte supports TypeScript 👌
svelte.dev/blog/svelte-and-typescript