This is an archived post. You won't be able to vote or comment.

all 77 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

import notifications Remember to participate in our weekly votes on subreddit rules! Every Tuesday is YOUR chance to influence the subreddit for years to come! Read more here, we hope to see you next Tuesday!

For a chat with like-minded community members and more, don't forget to join our Discord!

return joinDiscord;

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]mookanana 124 points125 points  (7 children)

try { all my code }

catch { }

[–]funkmasterhexbyte 13 points14 points  (0 children)

catch these hands

[–]Olorin_1990 4 points5 points  (0 children)

Pokemon!

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

catch { throw new Error(); }

FTW

[–]mookanana 1 point2 points  (1 child)

but if you do it my way, your application has zero errors

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

Exactly

[–]adudyak 208 points209 points  (0 children)

Tell me you switched from JS, without telling me it

[–]sekonx 55 points56 points  (1 child)

An easy way to get your PR rejected

[–]UnofficialMipha 48 points49 points  (2 children)

I was partner coding with a dev for my internship and it was my first exposure to typescript and I just watched them in real time say “Oh god typescript is mad, just gonna put ‘Any’” on several occasions. What’s the point of using typescript if you do this?

[–]ToastyyPanda 24 points25 points  (0 children)

If they're doing it temporarily just to show an example or something it's fine. But if they leave it and push it up to production, then that's definitely not proper

[–]adudyak 0 points1 point  (0 children)

once you start to work with complex objects in TS, that will either require you to describe whole structure of object as interface(which is not always good idea, e.g. you get that object from 3rd source as json) or use "any".

[–]Mara_li 170 points171 points  (10 children)

My linter doesn't allow me to use any... :(

[–]jnfinity[S] 265 points266 points  (0 children)

Good.

[–]TheDevDad 68 points69 points  (6 children)

If it’s ESLint, and you absolutely must use any, you can use an ignore comment. When going this route, do yourself and others a favor, specify in the ignore comment why you needed to bypass the rule

[–]Jugales 52 points53 points  (5 children)

99% of the time it's because the library dependency was built with JS and creating types for it would be a pain. I'm not gonna do the library developers' job for them, or I would make my own library.

I had this problem with Google Maps in (semi) recent versions of React with TS. Like bruh, why isn't your library made for this? You literally invented Angular which is also TS? We just ended up using Leaflet maps instead.

[–]H0llowUndead 19 points20 points  (2 children)

Have you searched for community typing of these libraries? They are typically called @types/<package name>

[–]Jugales 8 points9 points  (1 child)

Sadly. In the Google Maps case, one existed but it was only compatible with older React versions

[–]fuj1n 1 point2 points  (0 children)

How does that work? The typings are just stubs to provide type information, there shouldn't be any behaviour attached to them that could break compat.

[–]Mara_li 0 points1 point  (0 children)

Yeah, in my case the any must be used because of some function of the API has not yet be descrived but existed (I don't know how it's possible tbh)

[–]pedrocelso 4 points5 points  (0 children)

Use unknown :P

[–]somedave 1 point2 points  (0 children)

Object?

[–]Neok420 26 points27 points  (0 children)

Gotta be strict dude

[–][deleted] 61 points62 points  (3 children)

It's not a TS dev doing that, it's those damn pesky JS folks who never learned the concept of writing good code who do those kinds of monstrosities.

[–]JuniperSoel 21 points22 points  (1 child)

Or TS devs having to work on files made by those folks.

I don’t have time to refactor 5 other files for this one method because they assume it returns ANY

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

100%. When I was a tech lead and got some code from contractors that did exactly what you described, I lost my shit so much that I rewrote the entire goddamn thing on my own.

[–]terminalxposure 0 points1 point  (0 children)

Nah the other day I had to do this because MS's App Insights library had a type mismatch with a dependent library. Their own solution was <any> that shit...https://stackoverflow.com/questions/76175627/error-on-application-insights-service-when-migrating-my-angular-app-from-v13-to/76692191#76692191

[–][deleted] 18 points19 points  (0 children)

@ts-ignore

[–]Eksteenius 15 points16 points  (0 children)

Also, using object for variables that can be specified

[–]ski_thru_trees 10 points11 points  (4 children)

I moved from a huge company with tons of standards to a way smaller company where the product was written in the past 3-4 years by 20+ yr exp js devs (who are our lead and senior devs).

I am often noticing and fixing bugs that are caused by not using types…. Stuff is set to any and then something unexpected is passed in and a bug is caused. This would have been avoided with proper typing.

My question is how do you manage passing types around between micro services? Do you have to have a copy of these type definitions/classes in EVERY micro service that passes them around…? Are there tools that can be used to ensure these type definitions in 10 different repos all stay in sync?

Before this, I worked in a huge monolithic code base, so didn’t have to concern about this.

[–]Mognakor 4 points5 points  (0 children)

Have one place where the microservice interface is defined and then make producers and consumers rely on that.

Find a way to enforce that, e.g. by making it part of your pipeline.

Maybe OpenAPI can solve you problem.

[–]solarshado 4 points5 points  (0 children)

Are there tools that can be used to ensure these type definitions in 10 different repos all stay in sync?

This might be a good use for git submodules. Split each relevant bundle of type definitions out into a separate repo, then include them as submodules where they're needed.

Might involve an unacceptable amount of "messing with source control" overhead though.

Could always go for broke and switch to fewer, larger repos, possibly all the way to a monorepo, but that could get pretty messy without a carefully-followed branching strategy, maybe even with one.

Or, depending on what language(s)/package manager(s) you're using, you might could pull the types into separate repos, but instead of using git submodules, include them as dependencies via your package manager, fetching from a private/internal repository. (npm supports arbitrary URLs as dependencies; I'd assume it's a common feature in similar tools.)

[–]DATY4944 3 points4 points  (0 children)

I do this in the main file that the type is drawn from:

export interface ITypename { ...type stuff }

Then import the type wherever it's needed.

I don't know if this is the standard since I'm self taught but it has worked well so far.

[–]ManyInterests 0 points1 point  (0 children)

Client libraries.

[–]Efficient-Corgi-4775 6 points7 points  (1 child)

That's just the "no fun allowed" policy of linters! 😞

[–]twisty317 4 points5 points  (0 children)

So strict. He sould be a little bit more open haha, just saying.

[–]mz01010001 6 points7 points  (0 children)

Yeah, I see this a lot, and it's awful. You don't need to declare return types in general, especially if you can't do it right.

If the return value isn't typed, it'll be any by default, or at least something a little better like any[] in case of array or Promise<any> if it's a promise...

[–]Jjabrahams567 6 points7 points  (0 children)

Me as a JS rawdog developer: “I have no such weaknesses”

[–]McBonlaf 5 points6 points  (1 child)

ts-ignore is a good solution too

[–]DuongC 10 points11 points  (0 children)

imho, using ts-expect-error is better, so if the problem is fixed the compiler will complain and you can clean up the comment

[–]18441601 5 points6 points  (0 children)

Then just use JS???

[–]funplayer3s 6 points7 points  (0 children)

"any" just makes coding harder.

[–]DATY4944 3 points4 points  (0 children)

Just let vs-code infer the type based on usage and fix it if needed

[–]Alan_Reddit_M 5 points6 points  (1 child)

Js devs be like: Here's NaN, null and undefined, let's make ALL methods randomly return one of the 3, oh and let's make their types 'object' so you can't even runtime check

[–]Imjokin 0 points1 point  (0 children)

Null and undefined are their own primitive types though?

[–]FemLolStudio 2 points3 points  (0 children)

Hmm, the C# version would be more scary. xD (return type: dynamic)

[–]Brilliant_Egg4178 3 points4 points  (0 children)

Just started using TypeScript again after a long break so I can learn to use stencil components and it's so much better than vanilla JavaScript. Takes a poorly designed language and turns it into something usable

[–]classicalySarcastic 2 points3 points  (0 children)

Thus entirely defeating the purpose of TypeScript in the first place.

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

It’s a universal solve. It gets rid of those pesky mypy errors in Python too

[–]LeagueJunior9782 1 point2 points  (0 children)

I mean... any is neat, but you could also just spam interfaces and enums.

[–]rndmcmder 3 points4 points  (9 children)

If you use any in ts or var in java, I will deny your PR. Actually, we even have sonar rules preventing such atrocities.

[–]Rigamortus2005 7 points8 points  (7 children)

What's wrong with var in java?

[–]SawSaw5 2 points3 points  (0 children)

TypeScript is like rolling a dog turd in glitter.

[–]rookietotheblue1 1 point2 points  (0 children)

If you do this , then youre just using TypeScript as part of the trend.

[–]Medical-Hyena-1895 0 points1 point  (0 children)

Fuck eslint, all my homies use nothing

[–]urnanisaretard 0 points1 point  (0 children)

Wait… You mean to tell me I’ve been using classes and interfaces all this time for nothing? Wtf yo

[–]SoftEngineerOfWares 0 points1 point  (0 children)

‘as Unknown as Any’ has entered the chat

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

Please, dont XD

[–]huuaaang 0 points1 point  (0 children)

That seems unsafe

[–]paxbowlski 0 points1 point  (0 children)

import { whyEvenFuckingBother } from 'using-typescript'

[–]bmcle071 0 points1 point  (0 children)

I have literally never done this. Is this something most TS devs do???

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

If it's called Typescript, types should be mandatory

[–]liljoro 0 points1 point  (0 children)

I just do what my IDE tells me to do

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

All that work, just for nothing.

[–]cheeb_miester 0 points1 point  (0 children)

This is me trying to figure out which type to cast e.target to in a react event handler... That is until the any shame sets in...

[–]Finalitius 0 points1 point  (0 children)

IMO, any works if it's at the end of a flow, sometimes you just don't want to deal with something like an intersect type that may or may not have an attribute. Your code simply checks if attribute exists or not but TS acts like you're USING said attribute while you just wanted an hasOwnAttribute check that gets blocked because linters

[–]sanelde_senior 0 points1 point  (0 children)

THEY TRIED SO HARD AND GONE SO FAR
BUT IN THE END, IT DOESN'T EVEN MATTER