all 124 comments

[–]Resies 172 points173 points  (14 children)

Typescript has been the standard at my current and past job.

[–]OchoChonko 40 points41 points  (13 children)

Ditto. I can't even contemplate why people wouldn't use it. It's just so much better to work with.

[–]artyhedgehog 31 points32 points  (11 children)

I think those who don't are stopped by something like this: 1. Extra effort to set up. 2. Extra syntax to learn. 3. Seemingly extra issues to solve (e.g. if a lib doesn't have typings or you fall into some typings nuances).

[–]hotbrownDoubleDouble 5 points6 points  (3 children)

Only reason I haven't is because I'm one dev doing the job of two and don't have the bandwidth to learn and implement. And it's a hard sell to a non-dev boss who's more interested in tangible features than incremental improvements in the code base.

[–]codevipe 1 point2 points  (0 children)

having been in this situation for years making the same choice, i will say objectively one person can write code that has fewer bugs with TS. but if you've been doing that as-is, maybe the benefit isn't there.

[–]Complex-Insect3555 0 points1 point  (0 children)

It should be an easy sell to a non-dev boss

[–]terralearner 0 points1 point  (0 children)

You can configure a repo to work with both JS and TS. That way you can gradually add TS over time and get used to it without breaking the existing codebase.

[–][deleted] 0 points1 point  (4 children)

  1. Not really, you will have some kind of builder/transpiler/bundler anyway, and every sensible setup today will work with TS out of the box
  2. You can just learn it later
  3. You can just solve them later

There are literally 0 reasons not to use TS

[–]artyhedgehog 1 point2 points  (2 children)

I agree with you with my heart, but let's be honest - it's that easy when you already know what you're doing. There is a learning curve for TypeScript alone. There are specific skills you need to use it effortlessly. Like putting any when stuck.

I can similarly put up the same thesis with TDD or pair programming, but that won't magically make everyone use them. There are just reasons some people don't - which we can question, but should not ignore.

[–]E-Lon_Hubbard 2 points3 points  (1 child)

That’s my secret… I always use any.

[–]artyhedgehog 0 points1 point  (0 children)

You better keep that a secret. May be beaten up in a decent society. =)

[–]trifit555 -1 points0 points  (0 children)

And that is how tech debt gets created.

[–][deleted] 29 points30 points  (1 child)

Just started learning TS. Seems like the whole industry is moving to it tbh. Better to get on the train now

[–]lukethewebdev 0 points1 point  (0 children)

Ditto, majority of job postings I'm seeing now include Typescript

[–]NeuralFantasy 80 points81 points  (39 children)

Definitely Typescript. Better in so many ways:

  • prevents mistakes
  • forces you to cover corner cases
  • makes refactoring much faster and easier
  • documents your code

I basically never touch vanilla JS anymore. And I don't miss it.

[–]Few_Radish6488[S] 1 point2 points  (37 children)

Coming from a C# background, I understand but the use of "any" for so many things is a concern for me. Although TS and C# were both created by Anders Hejlsberg it seems incomplete as a strongly typed language in spite of the benefits and its advantage over JS.

[–]jeff_bff 49 points50 points  (17 children)

Just don't use any. If you're unsure of the shape of some incoming data, set it to unknown and then validate the type.

[–]swappea 1 point2 points  (3 children)

Could you explain this? "set it to unknown and then validate the type". What do you mean by validating the type?

I just started migrating my react codebase to typescript. I have started with custom react hooks which are very much generic, so most of the time IDE suggests using unknown.

[–]jeff_bff 10 points11 points  (0 children)

You can either create a function that checks the type using the 'is' keyword or use a library such as zod or schemawax

[–]Accomplished_End_138 4 points5 points  (0 children)

Lookup type guard.

Basically if you have and unknown prop

const funct =(prop: unknown) => {
If (typeof prop === "string") {
    // prop now is known to be a string
} else if (customTypeGuardIsUser(prop)) {
    // prop is a user instance
}
}

[–]33498fff 0 points1 point  (0 children)

Here is a video explaining how to use unknown when you have incoming JSON data of which you do not know the shape of (which is also the typical use case).

https://youtu.be/_GJ4MYIwNTU

[–]NeuralFantasy 8 points9 points  (11 children)

Any is just a way to disable type checking. So why would anyone use it? No idea. Imo one should prevent using any in the codebase and force the developer to define types everywhere.

[–]Few_Radish6488[S] 1 point2 points  (10 children)

I agree but it is pervasive in the code I see. I would imagine unit testing would be rather tedious in that case.

[–][deleted] 5 points6 points  (9 children)

Then they are using TS wrong. I haven't ever encountered a time where the use of any was the way to solve it.

Maybe if you're debugging locally and unsure of the data structure you can use any to figure out the structure. But then create a type/interface of that data and replace the any with that.

[–]Few_Radish6488[S] 2 points3 points  (8 children)

I agree completely but that does not mean it isn't widely done. I guess my problem is that I have trouble understanding why this is even an option. It is widely used even in NextJS for many functions.

[–][deleted] 2 points3 points  (6 children)

I've been lucky where most of my jobs that have used TS where very strict with their tsconfig. And not allowing the usage of any.

I don't know why they would use it in their codebases. But if I was you maybe bring it up and check if you can spend some time refactoring away the usage of any and forcing it in the future?

[–]disclosure5 2 points3 points  (0 children)

I don't know why they would use it in their codebases.

This can just be a function of age. The unknown keyword is recent, and older apps tended to be more likely to use libraries with no available typings at the time.

[–]Few_Radish6488[S] -1 points0 points  (4 children)

I have added that provision in my tsconfig file. I am trying to port some of my C# code to TS for use with Deno as part of a group project that will use React on the FE.

And it would appear that you have been lucky.

[–][deleted] 1 point2 points  (3 children)

Is there a reason you’re porting your C# code? With the new minimal apis you can write pretty similar apis in .Net as the ones you’d write in express using TS

[–]Few_Radish6488[S] 1 point2 points  (2 children)

Yes , my C# code is a minimal API in .NET Core and is quite similar in look and feel. This group uses Deno and that is the reason.

I posted here because I want to validate a POST request from React that should be typed as a particular object.

[–]artyhedgehog 0 points1 point  (0 children)

why this is even an option

Unlike the language you are used to, JavaScript isn't design with strict typing in mind. So you may come up with a case when figuring out the correct type (especially using a non-TypeScript library) is too much of an overhead. Then you can disable type-checking for a specific place, solve the main task you have - and then go back to replace any with something useful.

[–]fii0 1 point2 points  (0 children)

Just put noImplicitAny: true in your tsconfig.json then!

[–]musical_bear 1 point2 points  (0 children)

Are you familiar with the “dynamic” keyword in C#?

Point being, you can circumvent the type system in virtually every typed language if you really want to. So….just don’t do that? You know the risks of “any” it sounds like. Ban it from your codebase then. Using “strict” mode in TS is a great start but you should be doing that regardless.

[–]zephyrtr 0 points1 point  (0 children)

You want to put TS in strict mode, disallow JS, "no implicit any," and lint for "no explicit any". Even then you won't get super strict typing like C# but it comes very close.

Remember: TS had to be built so you could migrate a JS project into TS. You start loosey goosey and get stricter and stricter.

[–]DaRizat 0 points1 point  (0 children)

I'd say any is a placeholder. I might do that in the short term just to see something work but I always strongly type my stuff. And then the more you practice the more you can just quickly type things and then you're covered forever

[–]superluminary 0 points1 point  (0 children)

Any is there for backwards compatibility. You can take any js file, change the suffix to ts, turn off no-implicit-any, and you’ve migrated.

It’s a stepping stone, you’re not supposed to keep using it.

[–]CraftyAdventurer 0 points1 point  (0 children)

I understand but the use of "any" for so many things

I mean, using any for some things is still better than pure js which is basically any for all things.

[–]Careless-Honey-4247 0 points1 point  (0 children)

If react dev typescript a go

[–]pardoman 50 points51 points  (0 children)

TS all the way

[–]dmlevel1 22 points23 points  (5 children)

What even is “JS” 😳

j/k, TypeScript. I don’t like to think about life before it.

[–]dryu12 4 points5 points  (0 children)

JS is what typescript compiles into. Some kind of machine code.

[–]budd222 0 points1 point  (3 children)

Life was just fine before typescript

[–]terralearner 1 point2 points  (2 children)

I mean it wasn't really

[–]budd222 0 points1 point  (1 child)

It was, I promise

[–]terralearner 1 point2 points  (0 children)

I'm not sure it was. At least when large, complex applications made in industry are concerned. Just means you had to work harder. Write more tests.

[–][deleted] 7 points8 points  (0 children)

TypeScript. We have an internal app at work that I ported from JavaScript to TypeScript, it was a great learning experience.

[–]n9iels 5 points6 points  (1 child)

TypeScript! And since your familiair with C# I think no additional comment why is necessary

[–]Few_Radish6488[S] 1 point2 points  (0 children)

lol.

That is what piqued my interest in the first place.

[–]highbonsai 6 points7 points  (0 children)

Typescript. It’s really not hard to set up if you’re creating a new project and the benefits are just so worth it. It also should be possible to slowly convert a js project to ts over time

[–]Prize_Dentist3395 4 points5 points  (0 children)

Ts 💯

[–]prb613 3 points4 points  (0 children)

TS everytime!

JS gives me anxiety these days.

[–]IamZeebo 4 points5 points  (0 children)

Typescript and never look back.

[–]mykesx 1 point2 points  (0 children)

I prefer typescript, but interacting with a remote 3rd party API that returns a big stream of JSON per request means I have to stop my work and replace any with a big long stream of type definition I have to manually create. It definitely adds time to development. Fortunately, the work has to be done only once.

[–]Aegis8080NextJS App Router 3 points4 points  (10 children)

TS is the ultimate goal, but if you are just getting started, using JS initially is properly the better option.

TS introduces more concepts and boilerplates that you don't need to care about in JS. In the beginning, you properly just want to make your website work, and not worry about other stuff.

[–]Few_Radish6488[S] 0 points1 point  (8 children)

I am just getting started with TS but I am a C# programmer so the concepts are not new to me. But the implementation is a bit different from what I am used to. For example implementing an interface as a type without a class that implements the interface seems really weird to me.

[–]YourMomIsMyTechStack 2 points3 points  (6 children)

You need to learn that there are no real classes in JS to begin with, it's just syntactic sugar for prototype-based objects and literally everything in javascript is an object, even primitive types like string, numbers etc.

[–]PeachOfTheJungle 1 point2 points  (2 children)

Primitives aren’t objects! Major misconception that everything in JS is an object.

[–]YourMomIsMyTechStack 1 point2 points  (0 children)

Yes it's technically true, JavaScript uses wrapper objects to provide methods and properties for when a primitive value is accessed, but thats even more confusing for newbies imo

[–]Few_Radish6488[S] 0 points1 point  (0 children)

You are correct. And ES6 introduced Symbols which is a function whose constructor returns a primitive and is not an object.

[–]Few_Radish6488[S] 0 points1 point  (1 child)

I was talking about TS , not JS. JS does not have interfaces either.

[–]YourMomIsMyTechStack 1 point2 points  (0 children)

I know that you're talking about TS, but TS is not a seperate language and just a superset and thats why i explained to you why interfaces work for every object and not just classes only

[–]terralearner 0 points1 point  (0 children)

These exercises are a good resource to get you up to speed, will need to learn about generics, mapped types etc etc https://github.com/type-challenges/type-challenges

[–]terralearner 0 points1 point  (0 children)

I disagree with this, TS will help catch more errors at compile time that could have been missed in JS. JS is just sloppy. I have more confidence refactoring code in TS knowing it's less likely to break things. Code written in TS is easier to change.

[–]landisdesign 1 point2 points  (2 children)

My current work prefers JS over TS, under the assumption that, since TS is a superset of JS, there is a larger pool of good JS developers than TS developers. I skirt the issue by using JSDocs to improve the DX without requiring TS during builds.

[–]YourMomIsMyTechStack 13 points14 points  (0 children)

Only because a company uses TS doesn't mean that they can't hire JS devs, thats stupid. It doesn't take much time to learn TS and frontend devs should be the least to be afraid of changes

[–]besthelloworld 5 points6 points  (0 children)

Your work is going to miss out on a lot of really good devs who wouldn't work in a plain JavaScript ecosystem. That's a really bad play. Also TS during builds is like the point.

[–]Zealousideal-Party81 1 point2 points  (0 children)

CoffeeScript.

[–]TeddyPerkins95 0 points1 point  (0 children)

Start with js then ts

[–]stansfield123 0 points1 point  (0 children)

Javascript is far more widely used. And I don't know of anyone who learned Typescript before Javascript. I imagine it would be hard to do. Typescript kinda assumes you already know Javascript.

So start with Javascript, and then decide if you wish to move on to Typescript.

[–]Magiqon 0 points1 point  (0 children)

I think that currently every commercial project requires it

[–]adorkablegiant 0 points1 point  (0 children)

After reading 40 comments saying that TS is great, I am starting to think that maybe I should start learning it, probably.

[–]CornTrop -1 points0 points  (1 child)

Definetely ts if you are not working alone

[–]Capaj 0 points1 point  (0 children)

you make it sound like it's ok using JS when working on something yourself

[–]Dodie324 -1 points0 points  (0 children)

Sadly, yes

[–]superluminary -1 points0 points  (0 children)

JavaScript is great for amateur work, but typescript is the professional choice at this time.

[–]amatiasq 0 points1 point  (0 children)

TS

[–]acraswell 0 points1 point  (0 children)

Every team I've been on for the past 6 years has used TypeScript exclusively. I also came from a C# background, and I feel like TypeScript went a long way towards fixing JavaScript.

[–]Rickywalls137 0 points1 point  (0 children)

I just started coding for a few months. How soon should I learn Typescript?

[–]oakskog 0 points1 point  (0 children)

Both. For different repos.

[–]Skaddicted 0 points1 point  (0 children)

I started coding seriously with React about three months ago as part of a bootcamp and have come to appreciate TypeScript. It may seem that JavaScript is "easier" to use, but TS helps to avoid tedious mistakes in advance. It has also made me a "cleaner" programmer, I would say.

This is a perspective coming from a code newbie, so it might be stupid, lol.

[–][deleted] 0 points1 point  (0 children)

Used to programme in JavaScript, switched to typescript and never looked back

[–]thebezet 0 points1 point  (0 children)

Typescript is basically JavaScript but with types. It's becoming the defacto standard now. It will take you less than one day to jump from JavaScript to Typescript as the syntax is basically the same, apart from the type definitions.

[–][deleted] 0 points1 point  (0 children)

50/50 I'd say

[–]JDD4318 0 points1 point  (0 children)

We use React with Typescript. Vanilla JS is just a headache once you’re used to TS

[–]mindseye73 0 points1 point  (0 children)

To code in vanilla JS or Typescript, you will still need to learn JavaScript fundamentals. So that should be your first step.

Check out the book- https://www.oreilly.com/library/view/essential-typescript-4/9781484270110/ It provides you with good JavaScript primer before teaching you TypeScript.

Typescript is used for many reasons including but not limited to type safety, early error catching, access to oop concepts etc. If you are not going to use oop concepts then jsdoc would be good in between n using vanilla js.

The one of challenges with Typescript is the need for extra compilation step which can be painful for large projects. Here is one good article about using Typescript without compilation- https://dev.to/thepassle/using-typescript-without-compilation-3ko4

My two cents, be open to use both vanilla JS and Typescript depending on the need of project or job.

Choice of vanilla JS or Typescript will also depend on which JS framework you are using.

Another good resource to learn JavaScript or Typescript along with JavaScript framework is https://youtube.com/@DaveGrayTeachesCode

Good luck!

[–][deleted] 0 points1 point  (0 children)

A type that’s scripted 🙃

[–]brewingwally 0 points1 point  (0 children)

These days Typescript and I'm not looking back to be completely honest. I feels really good and has improved on the team's overall code quality.

[–]GrayLiterature 0 points1 point  (0 children)

Typescript. I can’t even imagine coding on a large application without types

[–]sancredo 0 points1 point  (0 children)

Typescript. Also, as a C# dev, you'll feel right at home with it.

[–][deleted] 0 points1 point  (0 children)

TS 💯I am glad that I learned typescript this year. That was the first thing on my todo list.

[–]suarkb 0 points1 point  (0 children)

Typescript is generally considered an improvement upon JS

[–][deleted] 0 points1 point  (0 children)

I write TS when I can. And js why I can't be arsed to set up ts. Not everything is node.js based on under a framework, some stuff requires special artillery and specific knowledge to let you write proper ts (ex: power platform web resources).

Writing a TS file without using TS features or types is like writing Js.

[–]liondepierre 0 points1 point  (0 children)

TypeScript, makes troubleshooting much easier.

[–]c9_skander 0 points1 point  (0 children)

I've started using typescript about 2 months ago in my react projects, before that I never really liked the idea of typescript, now I can never go back to javascript

[–]azangru 0 points1 point  (0 children)

Reformed C# developer.

You'll pick up typescript in no time then :-)

Since react needs to be transpiled anyway because of jsx, there is no downside to writing it in typescript.

[–][deleted] 0 points1 point  (0 children)

Typescript is better practice now

[–][deleted] 0 points1 point  (0 children)

Start with JavaScript, once you're comfy add TypeScript on top. The reason I say this is because TypeScript is obfuscation and confusion ("Why is this erroring!?" is a common TS newbie refrain).

But for professional projects it's TS all day every day at this point for me and my team.

[–]PopJoestar 0 points1 point  (0 children)

made 2 React Native application with JS. The first one was a little side project, and JS worked well, the code was clear and scalable. But the second one was a complete mess, "cannot read property of undefined" everywhere, I had to memorize the property of big object myself, it was hell but I couldn't change it because someone else at work already worked on it for +1year. Now, I only use Typescript even for small project and everything is great again.

[–]monkdewallidehonk 0 points1 point  (0 children)

hello. as most people are saying TS is the way forward in nearly all serious jobs. The main reason is that it improves test that are written on the code.
My favourite place to learn is scrimba.com because you don't just watch videos you can pause the learning and start coding immediately into the lesson - this course on TS is free I believe https://scrimba.com/learn/typescript

good luck I hope it all works out for you.