I'm a Python developer and this strictly typed langauges this something I left after I finished my Diploma (C, C++, Java)
I later got into React and all those web dev stuffs that the kids are into. At that time javascript was all you really needed. Then the wave of Typescript hit and everyone jumped on.
I get all the "advantages" of it, but in the past few months I looked into it and decided to rebuild my portfolio with it - the portfolio is still not done.
Most of my time was spent making Typescript happy and not building.
So, you Typescript pundits, why must I use it?
Top comments (60)
All I can do is to promise you'll thank every developer (including your past self) if they write their code with static types.
Don't like TS for typing? Maybe you'll prefer JSDoc: typescriptlang.org/docs/handbook/j...
Granted, I could have chosen better wording. Most of the times I come across people hyping TS it's usually "it'll make you faster". The way I work, fast is not a metric I focus on. But using that, I have spent more time doing something in typescript than I would have if It were just JavaScript.
It's not that I don't like it, I'm just not convinced more than anything. JSDoc you say, I'll have a look at it .
Thank you
I like the idea of JS Doc TS but in my judgement you have to be good with TypeScript first before trying to tackle JS Doc TS (and it has an even larger hurdle for initial setup). The best JS Doc TS example I know of is Preact.
Oh boy....
The thing here is - it does make you faster on 'common' development projects. This implies, you:
I think not being convinced is okay. My portfolio is written in JS as well. And I see no good reason to rewrite it just for the sake of adding Typescript to the codebase. The last two hackathons on dev.to, I wrote using Typescript. I didn't work on them regularly. Since they use several layers of abstraction, VSCode's TS intellisense was really helpful to understand
That's what I meant about the "you'll thank every developer later" part. The less mental capacity you use memorising what you built earlier, the easier it is to plan what to write next. Or whether you can re-use or refactor your modules.
This makes sense. When I do work on a project that uses it, then I'll dedicate more time to. As of now, I don't have a valid reason...
The whole affair drove me to build the portfolio with Eleventy instead - to actually see what javascript is necessary for it. For that, I'm grateful
Last time I checked there were additional hurdles to using TypeScript with 11ty.
Enough for me to consider using Astro instead if TypeScript was a critical consideration.
Oh no, Eleventy was after... I was initially building in Next.js with TS..
Then the TS thing made me decide to forgo frameworks altogether
My recommendation would be to not let it slow you down. Make good use of the typescript config file, ensuring that the strictness is calibrated to the level you need. It's phenomenally productive to catch sneaky bugs at compile time, even if it may not seem that way on the front leg of dev.
The awesome thing about the ts-config compiler options is that you can scale them up over time. The goal is to have full strictness enforced, but if and when that slows you down then by all means dial it back. The goal is DX and developer productivity, it just so happens that specifying types at compile time do allow for huge amounts of preventative measures
See, no one ever mentioned this to me - that I can choose how I want to work.
I will look into making it work for me and not the other way around.
This makes more sense
I'm not particularly fond of TypeScript - overall I view it as having poor ROI but decided to learn it anyway so that I could at least have a somewhat coherent base to criticize it from.
That said I accidentally fell into an interesting work flow. I was trying to use uvu with TypeScript. It uses tsm to test TypeScript code.
With that setup you can actually focus entirely on getting your unit tests done first without paying any attention to TypeScript - type checking (linting) only occasionally when the test results don't make any sense (i.e. overall the tests where more effective pointing out issues than TypeScript).
Once you're done with a unit of work you can then focus on what TypeScript is complaining about - "oh yeah, I better clean that up".
That allows a "JavaScript style" work flow with (a sprinkling of) explicit TypeScript types for documentation.
Basically use fast non-checking transpilation with a fast loader for reqular work and only run the full type check at the end or when things get weird.
This is realty interesting... Is there a public repo I can look at?
Not sure what you are hoping to find but recently I put together solid-hackernews-a.
Vite uses esbuild for "discarding type annotations" - so
npm run dev
doesn't type check (presumably that is supposed to happen a priori in the IDE/language server enabled editor).npm run lint:types
is used separately for static type checking.Not looking to find anything specific. I understand technologies better by having a look at how people use them over reading the docs...
I'll have a look, thank you
Once you get the "hang of it" you won't be battling with it. It will become second nature to build variables and type them.
By doing so you won't see as many type errors and such. It's just a learning curve. Persevere and you will seen the benefits !
I'll keep at it, without a doubt... I wanted to hear what other people say about it
Thank you
TypeScript has a fairly large learning curve (I would say larger than some real statically typed languages like C# and Java) and it WILL slow you down on small projects. It's also easy to use TypeScript poorly, and not get much benefit out of it, if you just throw
any
at any sign of a problem.But when you get into large projects, especially when you're working with other people, it will accelerate you and make your code better (if you use it well, anyway). Refactoring gets much easier, intellisense gets much better, you know what kind of data you're working with.
Some people are all-in on TypeScript and use it for everything. I'm not one of them - I find it a waste of time for relatively small, personal projects. But I'd never consider using plain JavaScript for anything substantial anymore.
Oh yes the
any
thing was the first thing I took to heart when i watched Jack Herrington's series. To that effect, i understand not to throw any everywhereThis is understandable, thank you for the explanation
My argument for typescript has 2 parts:
My own personal reasons for using typescript involve being able to be self-accountable and make sure all the contracts I write with the code are properly handled. It can give you some freedom when writing internal code, so you don't need to necessarily write tons of runtime checks on the data since you know the compiler will check for a lot of that. The only areas where data checking is really important in typescript is when you have an external data source (or input) that could come in during runtime that can possibly not meet the structure contract. As you adapt to the nuances of the flavored language, it gets easier and faster to build.
The biggest evidence of your struggle not being with the language is that the code you write does not obey the rules you write, so its more of an issue of how you are writing the code, not with typescript itself. Think of it like training wheels. Eventually with enough coding in typescript, writing a specific way will become second nature and you can take those practices with you into JavaScript.
Contacts? Web3?
I'm not foreign to typed languages, Typescript has just given me more grief than the other ones.
I wanted to understand the increasing adoption of it
For many, the fact that javascript isn't typed keeps people from working on web projects, and typescript was a way to bridge that gap. Being that it was developed by Microsoft, perhaps it was developed to bridge the gap between C# and web so they could reuse developers from backend on frontend to a degree, for familiarity.
Typescript is simply the same Javascript but extended with typing, but not only that. You’ll become better JS dev, with the full picture of this technology whether you use it widely or not, OOP, style of coding, tests, security, new features (ES2022..) and etc.
You do not "have to" use it. You would use it, because from what it offers on top of JavaScript you could think it is worth it. There is no need to use it, because you "have to". Typescript bascially is just an extension of JavaScript and allows you to use strict typing, which would make it comparable to lanuages like C#. Editors can make use of it and make development easier (show you allowed methods etc.) as well as allow you to integrate these types as a kind of "rules", which will lead to code, which does not get built, if you break these "rules". There would be no error during the execution in this specific cases, if you could not use it this way. If you want to use some of these points, which I just have described, for your project, you can see them as a must.
But you are not forced to used it and if you are still learning JavaScript, then stick to it for some time before you add with TypeScript to the language's complexity (my opinion at the moment).
Yeah, I'm noticing my wording could have been better (writing while tired)
My question was more on the "why" I see a number people advocating the switch to it. My question comes as my JS is solid enough to do what I need, so why would consider adding TS to the mix is all
Thank you for the elaboraion.
But I am also sorry, if my answer has been too serious. Thanks for your kind response.
It's alright... I got your thoughts as well as valuable information - which was my intention.
Adding types to your code in general is tremendously helpful when working on larger projects, regardless of the language. I've found static analysis tools like Psalm incredibly useful in PHP as well - it's found an awful lot of problems in legacy code.
There's a few options for adding types to JavaScript and while I have used Flow in the past, and still prefer it conceptually to the idea of a whole new language, the tool support is significantly better in Typescript. There are some things Flow does better (opaque types are frustratingly still not a thing in Typescript), but overall Typescript is the better experience.
I hear you Brother. I've been told right here on Dev.to that I'm dumb, stupid, illogical, and many other pejoratives. They all were Javascript only purists who even denounced their own language adopting classes. These are the same folks that write 5,000 line mono repos filled with magic strings who don't follow DRY. Talking about insanity.
You should only use typescript if you care about the mental wellbeing of whoever has to maintain the code after you're done with it.
Btw, you can always make typescript happy by typecasting to
unknown
firsts and thenany
. Not saying you should do this in production code, because you essentially lose all the benefits of typescript if you do this, but it can be helpful while learning. Once you get your code running, you can revisit the ugly type castings and determine how much time you want to take to "fix" it (spoiler: its usually much easier than you think and gets even easier as you learn more about the typescript typing system).It also helps to use packages that have proper typings, or that have associated @types packages. You don't usually have to do any typing when packages are properly typed.
Oh and another thing people usually run into: